Presentation is loading. Please wait.

Presentation is loading. Please wait.

Sorting Algorithms.

Similar presentations


Presentation on theme: "Sorting Algorithms."— Presentation transcript:

1 Sorting Algorithms

2 Selection Sort 36 24 10 6 12 values [ 0 ]
[ 1 ] [ 2 ] [ 3 ] [ 4 ] Divides the array into two parts: already sorted, and not yet sorted. On each pass, finds the smallest of the unsorted elements, and swap it into its correct place, thereby increasing the number of sorted elements by one. 36 24 10 6 12

3 Selection Sort: Pass One
values [ 0 ] [ 1 ] [ 2 ] [ 3 ] [ 4 ] 36 24 10 6 12 U N S O R T E D

4 Selection Sort: End Pass One
values [ 0 ] [ 1 ] [ 2 ] [ 3 ] [ 4 ] SORTED 6 24 10 36 12 U N S O R T E D

5 Selection Sort: Pass Two
values [ 0 ] [ 1 ] [ 2 ] [ 3 ] [ 4 ] SORTED 6 24 10 36 12 U N S O R T E D

6 Selection Sort: End Pass Two
values [ 0 ] [ 1 ] [ 2 ] [ 3 ] [ 4 ] SORTED 6 10 24 36 12 U N S O R T E D

7 Selection Sort: Pass Three
values [ 0 ] [ 1 ] [ 2 ] [ 3 ] [ 4 ] SORTED 6 10 24 36 12 U N S O R T E D

8 Selection Sort: End Pass Three
values [ 0 ] [ 1 ] [ 2 ] [ 3 ] [ 4 ] 6 10 12 36 24 S O R T E D UNSORTED

9 Selection Sort: Pass Four
values [ 0 ] [ 1 ] [ 2 ] [ 3 ] [ 4 ] 6 10 12 36 24 S O R T E D UNSORTED

10 Selection Sort: End Pass Four
values [ 0 ] [ 1 ] [ 2 ] [ 3 ] [ 4 ] 6 10 12 24 36 S O R T E D

11 Code for Selection Sort
void SelectionSort (int[] values) // Post: Sorts array values[0 . . numValues-1 ] // into ascending order by key { int endIndex = values.Length - 1 ; for (int current=0;current<endIndex;current++) Swap (values, current, MinIndex(values,current,endIndex)); }

12 Selection Sort code (contd)
int MinIndex(int[] values, int start, int end) // Post: Function value = index of the smallest value // in values [start] . . values [end]. { int indexOfMin = start ; for(int index =start + 1 ; index <= end ; index++) if (values[ index] < values [indexOfMin]) indexOfMin = index ; return indexOfMin; }

13 Bubble Sort values [ 0 ] [ 1 ] [ 2 ] [ 3 ] [ 4 ] 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. 36 24 10 6 12

14 Bubble Sort: Pass One 36 24 10 6 12 36 24 6 10 12 36 6 24 10 12 6 36 24 10 12

15 Bubble Sort: Pass Two 6 36 24 10 12 6 36 10 24 12 6 10 36 24 12

16 Snapshot of BubbleSort

17 Code for Bubble Sort void BubbleSort(int[] values) { int current = 0;
while (current < values.Length - 1) BubbleUp(values, current,values.Length-1); current++; }

18 Bubble Sort code (contd.)
void BubbleUp(int[] values, int startIndex, int endIndex) // Post: Adjacent pairs that are out of // order have been switched between // values[startIndex]..values[endIndex] // beginning at values[endIndex]. { for (int index = endIndex; index > startIndex; index--) if (values[index] < values[index-1]) Swap(values, index, index-1); }

19 Insertion Sort 36 24 10 6 12 values [ 0 ]
[ 1 ] [ 2 ] [ 3 ] [ 4 ] 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. 36 24 10 6 12

20 Insertion Sort 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. 6 10 24 36 12

21 Insertion Sort 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. 6 10 24 36 12

22 Insertion Sort 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. 24 36 10 6 12

23 Insertion Sort 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. 12 24 36 10 6

24 A Snapshot of the Insertion Sort Algorithm

25 Insertion Sort

26 Code for Insertion Sort
void InsertionSort(int[] values) // Post: Sorts array values[0 . . numValues-1] into // ascending order by key { for (int curSort=0;curSort<values.Length;curSort++) InsertItem ( values , 0 , curSort ) ; }

27 Insertion Sort code (contd.)
void InsertItem (int[] values, int start, int end ) { bool finished = false ; int current = end ; bool moreToSearch = (current != start); while (moreToSearch && !finished ) { if (values[current] < values[current - 1]){ Swap(values[current], values[current - 1); current--; moreToSearch = ( current != start ); } else finished = true ;

28

29


Download ppt "Sorting Algorithms."

Similar presentations


Ads by Google