Presentation is loading. Please wait.

Presentation is loading. Please wait.

Chapter 3: Sorting and Searching Algorithms 3.2 Simple Sort: O(n 2 )

Similar presentations


Presentation on theme: "Chapter 3: Sorting and Searching Algorithms 3.2 Simple Sort: O(n 2 )"— Presentation transcript:

1 Chapter 3: Sorting and Searching Algorithms 3.2 Simple Sort: O(n 2 )

2 2 Sorting means... Sorting rearranges the elements into either ascending or descending order within the array. (we’ll use ascending order.)

3 3 Sorting Putting collections of things in order –Numerical order –Alphabetical order –An order defined by the domain of use E.g. color, …

4 4 Stable Sort A concept that applies to sorting in general What to do when two items have equal keys? A sorting algorithm is stable if –Two items A and B, both with the same key K, end up in the same order before sorting as after sorting Example: sorting in excel –Given name, salary, department –Fred, Sally, and Dekai: dept == shoes, salary == 50K –Juan and Donna: dept == shoes, salary == 70K –In a stable sort: If you first sort by salary, then by dept, the order of Fred, Sally, and Dekai doesn’t change relative to each other Same goes for Juan and Donna

5 5 Stable Sort Sort by salary Order of names within category doesn’t change

6 6 Simple Sorting Algorithms O(n 2 ) 1.Selection Sort 2.Bubble Sort 3.Insertion Sort All these have computing time O(n 2 )

7 7 Divides the array into two parts: already sorted, and not yet sorted. On each pass, finds the smallest of the unsorted elements, and swaps it into its correct place, thereby increasing the number of sorted elements by one. 1. Selection Sort values [ 0 ] [ 1 ] [ 2 ] [ 3 ] [ 4 ] [ 5 ] 23 78 45 8 32 56

8 8 Selection Sort K 1.Swap smallest element with element # k 2.Move the wall one element forward

9 9 Example of selection sort

10 10 Example of selection sort

11 11 Selection sort algorithm

12 12 Selection Sort: How many comparisons? 23 78 45 8 32 56 values [ 0 ] [ 1 ] [ 2 ] [ 3 ] [ 4 ] [ 5 ] 5 compares for pass[1] 4 compares for pass [2] 3 compares for pass [3] 2 compares for pass [4] 1 compare for pass [5] = 5 + 4 + 3 + 2 + 1

13 13 For selection sort in general The number of comparisons when the array contains N elements is Sum = (N-1) + (N-2) +... + 2 + 1 (arithmetic series) O(N 2 )

14 14 /*SelectionSort Algorithm */ void SelectSort(Array A) { intk, x; for( k = 0; k < NElements; k++ ) { x = GetMinPosition(A,k, NElements-1); Swap(A[x], A[k]); } Swap (array A) { int temp = A(K); A(k) = A(x); A(x)= temp ; ========================================================== /*GetMaxPosition Method finds the element with the greatest value and returns its position */ intGetMinPosition (Array A, int i0, int i1) { intm = A[i0], x = i0; for( int i = i0 + 1; i <= i1; i++) { if ( A[i] < m ) { m = A[i]; x = i; } return x; } Selection Sort

15 15 Compares neighboring pairs of array elements, starting with the last array element, and swaps neighbors whenever they are not in correct order. On each pass, this causes the smallest element to “bubble up” to its correct place in the array. 2. Bubble Sort values [ 0 ] [ 1 ] [ 2 ] [ 3 ] [ 4 ] [ 5 ] 23 78 45 8 32 56

16 16 Bubble sort In each pass: 1.Compare the first to the second, the second to the third, and so on. 2. Do swap to keep smallest of each compared pair to left

17 17 Example of bubble sort

18 18 Example of bubble sort Requires at least n-1 passes

19 19 Worst-case analysis: N+N-1+ …+1= O(N 2 ) Bubble-Sort (Array a, int n) for (pass=0; pass<n-1; pass++) { for (j=n-2; j>=pass; j--) { if (a[j+1] < a[j]) {//compare the two neighbors tmp = a[j]; // swap a[j] and a[j+1] a[j] = a[j+1]; a[j+1] = tmp; } Bubble Sort Analysis if (a[j+1] < a[j]) Swap (a[j+1], a[j]) ;

20 20 /*BubbleSort Algorithm: Sorts array values[0.. numValues-1 ] into ascending order by key */ void BubbleSort ( ItemType values [ ], int numValues ) { int current = 0, end = numValues - 1 ; while ( current < end ) { BubbleUp ( values, current, end) ; current++ ; } ========================================================== /*BubbleUp: Neighboring elements that were out of order have been swapped between values [start] and values [end], beginning at values [end]. */ void BubbleUp ( ItemType values [ ], int start, int end ) { for ( int index = start ; index < end ; index++ ) if (values [ index +1 ] < values [ index ] ) Swap ( values [ index ], values [ index - 1 ] ) ; } Bubble Sort

21 21 One by one, each as yet unsorted array element is inserted into its proper place with respect to the already sorted elements. On each pass, this causes the number of already sorted elements to increase by one. 3. Insertion Sort 23 78 45 8 32 56 values [ 0 ] [ 1 ] [ 2 ] [ 3 ] [ 4 ] [ 5 ]

22 22 Works like someone who “inserts” one more card at a time into a hand of cards that are already sorted. To insert 12, we need to make room for it by moving first 36 and then 24. Insertion Sort 6 10 24 12 36

23 23 6 10 24 Works like someone who “inserts” one more card at a time into a hand of cards that are already sorted. To insert 12, we need to make room for it by moving first 36 and then 24. Insertion Sort 36 12

24 24 Works like someone who “inserts” one more card at a time into a hand of cards that are already sorted. To insert 12, we need to make room for it by moving first 36 and then 24. Insertion Sort 6 10 24 36 12

25 25 Works like someone who “inserts” one more card at a time into a hand of cards that are already sorted. To insert 12, we need to make room for it by moving first 36 and then 24. Insertion Sort 6 10 12 24 36

26 26 Insertion sort

27 27 Example of insertion sort

28 28 Example of insertion sort

29 29 Insertion sort 1) Initially p = 1 2) Let the first p elements be sorted. 3) Insert the (p+1) st element properly in the list (go inversely from right to left) so that now p+1 elements are sorted. 4) increment p and go to step (3)

30 30 Insertion sort - - - - - - Inner loop is executed p times (pm shifts in worst case), for each p=1..N  Overall: 1 + 2 + 3 +... + N = O(N 2 )

31 31 Insertion Sort: A program class Insertion_Sort { static void InsertionSort(int[] A, int n) { for (int i = 1; i < n; i++) { int v = A[i]; int j = i-1; while (j >= 0 && A[j] > v) { A[j + 1] = A[j]; j--; } A[j + 1] = v; }

32 32 /*Insertion Sort Algorithm that uses the InsertSorted method to sort the array */ void InsertionSort(Arr A2, int NElements) { intk, N = NElements; for( int i=1; i < N; i++ ) { k = A[i]; InsertSorted(A2, k, i); } =========================================================== /*InsertSorted Method add element according to its position that keeps the array elements in ascending ordered.*/ bool InsertSorted (Arr A2, int v, int last) { intk, kpos = last; boolfound = false; for( k=last-1; k >= 0 && !found; k--) { if (v <= A[k] { a[k+1] = A[k]; else a[k+1] = v; found = true; } if (!found ) a[0] = v; } Another Insertion Sort Program


Download ppt "Chapter 3: Sorting and Searching Algorithms 3.2 Simple Sort: O(n 2 )"

Similar presentations


Ads by Google