Download presentation
Presentation is loading. Please wait.
Published byMillicent Carson Modified over 9 years ago
1
CS 112 Introduction to Programming Sorting of an Array Debayan Gupta Computer Science Department Yale University 308A Watson, Phone: 432-6400 Email: yry@cs.yale.edu
2
Sorting 2
3
3 Roadmap: Arrays Motivation, declaration, initialization, access Reference semantics: arrays as objects Example usage of arrays m Tallying: array elements as counters m Keeping state Manipulating arrays m Sorting an array
4
4 Sorting an Array The process of arranging an array of elements into some order, say increasing order, is called sorting Many problems require sorting m Google: display from highest ranked to lower ranked m Morse code
5
5 Sorting in CS Sorting is a classical topic in algorithm design
6
6 Sorting an Array How do we sort an array of numbers? int[] numbers = { 3, 9, 6, 1, 2 };
7
Many Sorting Algorithms Insertion sort Selection sort Bubble sort Merge sort Quick sort … 7 http://www.youtube.com/watch?v=INHF_5RIxTE
8
8 Insertion Sort Basic idea: divide and conquer (reduction) m reduce sorting n numbers to sort the first n-1 numbers insert the n-th number to the sorted first n-1
9
9 insertPos =4 01234 insertPos =3 insertPos =2 insertPos =1
10
10 Insertion PseudoCode // assume 0 to n – 1 already sorted // now insert numbers[n] // insertPos = n; // repeat (number at insertPos-1 > to_be_inserted) { // shift larger values to the right // numbers[insertPos] <- numbers[insertPos-1]; // insertPos--; // numbers[insertPos] <- to_be_inserted;
11
11 // insertPos = n; // repeat (number at insertPos-1 > to_be_inserted) { // shift larger values to the right // numbers[insertPos] <- numbers[insertPos-1]; // insertPos--; // numbers[insertPos] <- to_be_inserted; 0123
12
12 Refinement: Insertion PseudoCode // assume 0 to n – 1 already sorted // now insert numbers[n] // insertPos = n; // repeat (insertPos > 0 && number at insertPos-1 > to_be_inserted) { // shift larger values to the right // numbers[insertPos] <- numbers[insertPos-1]; // insertPos--; // numbers[insertPos] <- to_be_inserted;
13
13 Insertion Sort Implementation public static void sort (int[] numbers) { for (int n = 1; n < numbers.length; index++) { int key = numbers[n]; int insertPos = n; // invariant: the elements from 0 to index -1 // are already sorted. Insert the element at // index to this sorted sublist while (insertPos > 0 && numbers[insertPos-1] > key) { // shift larger values to the right numbers[insertPos] = numbers[insertPos-1]; insertPos--; } numbers[insertPos] = key; } // end of for } // end of sort
14
14
15
15
16
16
17
17
18
18
19
19
20
Analysis of Insertion Sort What is algorithm complexity in the worst case? 20 int[] numbers = { 3, 9, 6, 1, 2 };
21
21 Sorting Arrays: Bubble Sort Scan the array multiple times m during each scan, if elements at i and i+1 are out of order, we swap them This sorting approach is called bubble sort m http://en.wikipedia.org/wiki/Bubble_sort http://en.wikipedia.org/wiki/Bubble_sort Remaining question: when do we stop (the termination condition)?
22
Sorting: Bubble Sort 22 public static void sort (int[] numbers) { boolean outOfOrder = false; do { outOfOrder = false; // one scan for (int i = 0; i < numbers.length-1; i++) { if (numbers[i] > numbers[i+1]) { // out of order // swap int x = numbers[i]; numbers[i] = numbers[i+1]; numbers[i+1] = x; outOfOrder = true; } // end of if } // end of for } while (outOfOrder); } // end of sort
23
23 Selection Sort For the i-th iteration, we select the i-th smallest element and put it in its final place in the sort list
24
24 Selection Sort The approach of Selection Sort: m select one value and put it in its final place in the sort list m repeat for all other values In more detail: m find the smallest value in the list m switch it with the value in the first position m find the next smallest value in the list m switch it with the value in the second position m repeat until all values are placed
25
25 Selection Sort An example: original: 3 9 6 1 2 smallest is 1: 1 9 6 3 2 smallest is 2: 1 2 6 3 9 smallest is 3: 1 2 3 6 9 smallest is 6: 1 2 3 6 9
26
26 Sorting: Selection Sort public static void sort (int[] numbers) { int min, temp; for (int i = 0; i < numbers.length-1; i++) { // identify the i-th smallest element min = i; for (int scan = i+1; scan < numbers.length; scan++) if (numbers[scan] < numbers[min]) min = scan; // swap the i-th smallest element with that at i temp = numbers[min]; numbers[min] = numbers[i]; numbers[i] = temp; } // end of for } // end of sort
27
Analysis of Selection Sort What is algorithm complexity in the worst case? 27 int[] numbers = { 3, 9, 6, 1, 2 };
28
Roadmap Both insertion and selection have complexity of O(N 2 ) Q: What is the best that one can do and can we achieve it? 28
29
Sorting: Merge Sort Split list into two parts Sort them separately Combine the two sorted lists (Merge!) Divide and Conquer! 29
30
Sorting 30 public void sort(int[] values) { numbers = values; // numbers has been previously declared mergesort(0, number - 1); } private void mergesort(int low, int high) { // check if low is smaller then high, if not then the array is sorted if (low < high) { // Get the index of the element which is in the middle int middle = low + (high - low) / 2; mergesort(low, middle); // Sort the left side of the array mergesort(middle + 1, high); // Sort the right side of the array merge(low, middle, high); // Combine them both }
31
Merging 31 private void merge(int low, int middle, int high) { // Copy both parts into the helper array for (int i = low; i <= high; i++) { helper[i] = numbers[i]; } int i = low, j = middle + 1, k = low; // Copy the smallest values from either side while (i <= middle && j <= high) { if (helper[i] <= helper[j]) { numbers[k] = helper[i]; i++; } else { numbers[k] = helper[j]; j++; } k++; } // Copy the rest of the left side of the array into the target array while (i <= middle) { numbers[k] = helper[i]; k++; i++; }
32
Merging 32 3 6 1 8 4 7 5 2
33
Sorting: Quick Sort Select a random element Compare it to every other element in your list to find out its rank or position You have now split the list into two smaller lists (if a > x and x > b, then we know that a > b – we don’t need to compare!) 33
34
Quicksort 34 private void quicksort(int low, int high) { int i = low, j = high; // Get the pivot element from the middle of the list int pivot = numbers[low + (high-low)/2]; // Divide into two lists while (i <= j) { // If the current value from the left list is smaller then the pivot // element then get the next element from the left list while (numbers[i] < pivot) { i++; } // If the current value from the right list is larger then the pivot // element then get the next element from the right list while (numbers[j] > pivot) { j--; }
35
Quicksort.. Contd. 35 // If we have found a values in the left list which is larger then // the pivot element and if we have found a value in the right list // which is smaller then the pivot element then we exchange the // values. // As we are done we can increase i and j if (i <= j) { exchange(i, j); i++; j--; } // Recursion if (low < j) quicksort(low, j); if (i < high) quicksort(i, high); } private void exchange(int i, int j) { int temp = numbers[i]; numbers[i] = numbers[j]; numbers[j] = temp; }
36
What’s the best we can do? N 2 ? N log N Why? 36
37
N log N N! possible outcomes If we compare two numbers, there are only 2 possible combinations that we can get So, if we have x steps, then we can produce a total of 2 x combinations To get 2 x > N!, we need x > N log N 37
38
Questions? 38
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.