CSCI 3333 Data Structures Simple Sorting Algorithms by
CSCI 3333 Data Structures Simple Sorting Algorithms by Dr. Bun Yue Professor of Computer Science yue@uhcl. edu http: //sce. uhcl. edu/yue/ 2013
Acknowledgement Mr. Charles Moen ¡ Dr. Wei Ding ¡ Ms. Krishani Abeysekera ¡ Dr. Michael Goodrich ¡
Bubble Sort Compare neighbor objects and swap if out of order. ¡ In each iteration, the largest remaining key (or smallest depending on how it is implemented) bubble up. ¡ Simple ¡ Slow ¡
Bubble Sort Algorithm: Bubble. Sort(A) Input: A: an array of objects to be sorted. Output: A is sorted according to its key values. N = A. Size for (i = 0; i < N-1; i++) // Each iteration places the largest remaining element in its right location for (j = 1; j < N - i; j++) if (A[j-1]. key > A[j]. key) Swap(A[j-1], A[j]);
Bubble Sort Example Original data sequence: 572693 ========= 527693 526739 ==End of Pass 1==== 25673|9 25637|9 ==End of Pass 2==== 2536|79 ==End of Pass 3==== 235|679 ==End of Pass 4====
Time Complexity Outer loop: N-1 iterations. ¡ Inner loop: j: N-1 downto 1 iteration: ¡ l l j key comparisons Up to j object swapping
Observation ¡ If there is no swapping in an iteration, the array is sorted.
Bubble Sort Algorithm 2 Algorithm: Bubble. Sort(A) Input: A: an array of objects to be sorted. Output: A is sorted according to its key values. N = A. Size i=0 do swapped = false // Each iteration places the largest remaining element in its right location for (j = 1; j < N - i; e++) if (A[j-1]. key > A[j]. key) Swap(A[j-1], A[j]) swapped = true end if end for i++ while (swapped or i < N)
Selection Sort ¡ Ideas: N-1 iterations. Each iteration: l l l Select the element with the smallest key value. Swap the element with the element in the front position. Work on the remaining array in the next
Selection Sort Algorithm: Selection. Sort(A) Input: A: an array of objects to be sorted. Output: A is sorted according to its key values. N = A. Size for (i = 0; i < N; i++) min = i for (j = i+1; j < N; j++) if (A[j]. key < A[min]. key) min = j end if end for Swap(A[i], A[min]) end for
Insertion Sort Each iteration sorts one element. ¡ The next element in the unsorted portion is inserted in the right location in the sorted portion. ¡
Insertion Sort Example Unsorted array 6274 4892 2843 9523 1892 Sorted Unsorted 6274 4892 2843 9523 1892 4892 6274 2843 9523 1892 2843 4892 6274 9523
Insertion Sort Algorithm Insertion. Sort(S) Input: S: array of comparable elements. Output: S is sorted. for i = 1 to S. Size-1 do curr. Item = S[i] j = i-1 while j >= 0 and S[j] > curr. Item do S[j+1] = S[j] j = j-1 end while S[j+1] = curr. Item end for
Time Complexity ¡ Best case: O(N) l ¡ Happens when S is already sorted. Worst case: O(N 2) l Happens when S is sorted in the reverse order.
Time Complexity Comparison Average and Worst case efficiency Best case efficiency Bubble sort O(n 2) Selection sort O(n 2) Insertion sort O(n 2) O(n) Algorithm
Other Properties Stable: original order of equal elements is preserved. ¡ Generally faster than selection sort and bubble sort. ¡ Additional memory requirement: O(1). ¡
Shell Sort ¡ ¡ ¡ Invented by Donald Shell in 1959. Improved on insertion sort by comparing elements separated by a gap. Elements separated by the gap are sorted. Gaps become smaller in every iteration. When the gap becomes 1, it becomes insertion sort and the array is sorted. Gap sequence: the sequence of gaps to be sorted.
Shell Sort Example ¡ ¡ Consider the array: {12, 21, 33, 47, 63, 55, 29, 70, 37, 11, 68, 90, 21, 53, 69, 70, 54, 29} Assume using the gap sequence of (5, 3, 1).
5 -sort Conceptually, before 5 -sort Conceptually, after 5 -sort: (7 swaps) Actual array: 12 55 68 70 21 29 90 54 33 70 21 29 47 63 37 11 53 69 12 55 68 70 21 29 54 90 21 29 33 70 37 47 53 11 63 69 {12, 21, 37, 11, 55, 29, 47, 63, 68, 54, 33, 53, 69, 70, 90, 70}
3 -sort 12 21 21 Conceptually, before 3 -sort 37 11 55 29 29 47 Conceptually, after 3 -sort: (6 swaps) 12 11 21 63 68 54 29 21 47 33 53 69 33 29 54 70 90 70 37 53 55 63 68 69 70 90 70 Actual array: {12, 11, 29, 21, 47, 33, 29, 54, 37, 53, 55, 63, 68, 69, 70, 90, 70}
1 -sort The array is now sorted. ¡ As an exercise, find out the number of swapping performed. ¡
Shell Sort Algorithm Shell. Sort(A) Input: A: array of comparable elements Output: A is sorted. for (gap = Initial. Gap(A) ; gap > 0; gap = Next. Gap(gap)) for (i = gap; i < A. Size; i++) // Insertion sort of all columns. // i starts with gap since A[0. . gap-1] // represent the first element in the columns curr. Element = A[i] for (j=i; j>=gap && A[j-gap]>curr. Element; j-=gap) A[j] = A[j-gap] end for A[j-gap] = curr. Element end for
Shell Sort Algorithm Initial. Gap(A) Input: A: array of comparable elements Output: The initial gap size used in Shell sort return A. Size/2 Algorithm Next. Gap(gap) Input: gap: current gap size Output: The next gap size used in Shell sort return gap == 2 ? 1 : (int) Math. round(gap / 2. 2)
Shell Sort Performance Depend on the gap sequence. ¡ Original sequence (initial size of ½ of array; next gap is half of current gap): worst case: O(n 2) ¡ Other sequences: worst case improved to O(n 1. 5) and O(n (lg n)2) ¡ Best known gap sequence: 1, 4, 10, 23, 57, 132, 301, 701, 1750. Faster than insertion sort and heap sort. ¡
Conclusion ¡ ¡ ¡ These sorting algorithms are (relatively) simple and not very efficient. Except of Shell Sort, average case complexity: O(n 2) There are other algorithms with average case complexity of O(n lg n). Examples: l l l Quick sort Merge sort Heap sort
Questions and Comments?
- Slides: 26