Lecture Notes on Design & Analysis of Algorithms

Only available on StudyMode
  • Topic: Sorting algorithm, Quicksort, Comparison sort
  • Pages : 11 (3413 words )
  • Download(s) : 241
  • Published : June 26, 2012
Open Document
Text Preview
Lecture Notes on Design & Analysis of Algorithms

G P Raja Sekhar Department of Mathematics I I T Kharagpur
Acknowledgements : Ahmad Abdullah, Pronoy Sikdar, Anshul Kamra, Sugam Agrawal & Students of DAA course 2006-07, Data Structures & Algorithms 2006-07.

The bubble sort is the oldest and simplest sort in use. The bubble sort works by comparing each item in the list with the item next to it, and swapping them if required. The algorithm repeats this process until it makes a pass all the way through the list without swapping any items. This causes larger values to "bubble" to the end of the list while smaller values "sink" towards the beginning of the list.

Pros: 1) Simplicity and ease of implementation. 2) Auxiliary Space used is O (1). Cons: 1) Very inefficient. General complexity is O (n2). Best case complexity is O(n).

Bubble Sort Efficiency

The insertion sort works just like its name suggests - it inserts each item into its proper place in the final list. The simplest implementation of this requires two list structures - the source list and the list into which sorted items are inserted. To save memory, most implementations use an in-place sort that works by moving the current item past the already sorted items and repeatedly swapping it with the preceding item until it is in place.

Pros: Auxiliary space used is O (1). Cons: General Complexity is O (n2). Best Case is O(n) when the list is already sorted.

Insertion Sort Efficiency

All elements to be sorted are inserted into a heap, and the heap organizes the elements added to it in such a way that either the largest value (in a max-heap) or the smallest value (in a min-heap) can be quickly extracted. Moreover, because this operation preserves the heap's structure, the largest/smallest value can be repeatedly extracted until none remain. Each time we delete (extract) the maximum, we place it in the last location of the array not yet occupied, and use the remaining prefix of the array as a heap holding the remaining unsorted elements. This gives us the elements in order.

Pros: 1) Time complexity of the algorithm is O (n log n). 2) Auxiliary Space required for the algorithm is O (1). 3) In-space and non-recursive makes it a good choice for large data sets. Cons:

1) Works slow than other such DIVIDE-AND-CONQUER sorts that also have the same O (n log n) time complexity due to cache behavior and other factors. 2) Unable to work when dealing with linked lists due to non convertibility of linked lists to heap structure.

Efficiency Of Heap Sort

Quick Sort
Quick Sort is an algorithm based on the DIVIDE-AND-CONQUER paradigm that selects a pivot element and reorders the given list in such a way that all elements smaller to it are on one side and those bigger than it are on the other. Then the sub lists are recursively sorted until the list gets completely sorted. The time complexity of this algorithm is O (n log n).

(Quick sort in action on a list of random numbers. The horizontal lines are pivot values.)

(In-place partition in action on a small list. The boxed element is the pivot element, blue elements are less or equal, and red elements are large.) . Pros: 1) One advantage of parallel quick sort over other parallel sort algorithms is that no synchronization is required. A new thread is started as soon as a sub list is available for it to work on and it does not communicate with other threads. When all threads complete, the sort is done. 2) All comparisons are being done with a single pivot value, which can be stored in a register. 3) The list is being traversed sequentially, which produces very good locality of reference and cache behavior for arrays.

Cons: 1) Auxiliary space used in the average case for implementing recursive function calls is O (log n) and hence proves to be a bit space costly, especially when it comes to large data sets. 2) Its worst case has a time complexity of O (n2)...
tracking img