Presentation is loading. Please wait.

Presentation is loading. Please wait.

Sorting Putting things in order. Searching and Sorting  Searching and sorting are two of the most used and most studied types of algorithms in computing.

Similar presentations


Presentation on theme: "Sorting Putting things in order. Searching and Sorting  Searching and sorting are two of the most used and most studied types of algorithms in computing."— Presentation transcript:

1 Sorting Putting things in order

2 Searching and Sorting  Searching and sorting are two of the most used and most studied types of algorithms in computing  In the early 70’s approximately 80% of computer usage was spent either sorting data or searching data  Examples:  Searching for a login name or PIN from a database of IDs  Searching for a web site on the internet  Sorting a list of potential stock purchases based on some performance criterion  Bank statement with transactions sorted by date

3 Searching and Sorting  Many methods for searching/sorting; w hich is the “best”?  The one that is the quickest  The one that uses the least amount of memory  Can sort in ascending or descending order.  Sorting  Bubble sort  Insertion sort  Selection sort  Merge sort  Quick sort  Searching  Linear search  Binary search

4 Selection Sort public void selectionSort (int[] data) { for(int i=0; i<data.length; i++) { int min = indexOfMinimum(data, i); int temp = data[min]; data[min] = data[i]; data[i] = data[temp]; } public int indexOfMinimum(int[] vals, int i) { int min = i; for (int j=i+1; j<vals.length; j++) { if (vals[j] < vals[min]) { min = j; } return min; } public void selectionSort (int[] data) { for(int i=0; i<data.length; i++) { int min = indexOfMinimum(data, i); int temp = data[min]; data[min] = data[i]; data[i] = data[temp]; } public int indexOfMinimum(int[] vals, int i) { int min = i; for (int j=i+1; j<vals.length; j++) { if (vals[j] < vals[min]) { min = j; } return min; } int[] nums = {3, 18, -1, 12, 6, -2, 2}; selectionSort(nums); int[] nums = {3, 18, -1, 12, 6, -2, 2}; selectionSort(nums);

5 Selection Sort Can we modify to sort arrays of objects? public void selectionSort (int[] data) { for(int i=0; i<data.length; i++) { int min = indexOfMinimum(data, i); int temp = data[min]; data[min] = data[i]; data[i] = data[temp]; } public int indexOfMinimum(int[] vals, int i) { int min = i; for (int j=i+1; j<vals.length; j++) { if (vals[j] < vals[min]) { min = j; } return min; } public void selectionSort (int[] data) { for(int i=0; i<data.length; i++) { int min = indexOfMinimum(data, i); int temp = data[min]; data[min] = data[i]; data[i] = data[temp]; } public int indexOfMinimum(int[] vals, int i) { int min = i; for (int j=i+1; j<vals.length; j++) { if (vals[j] < vals[min]) { min = j; } return min; } String[] words = {“hello”, “jim”, “rams”, “packers”}; selectionSort(words); String[] words = {“hello”, “jim”, “rams”, “packers”}; selectionSort(words);

6 Consider  How to sort a simple list?  Can we use the ‘array=based’ selection sort? void merge(SimpleList v1, SimpleList v2, SimpleList result) { v1.reset(); v2.reset(); result.reset(); while(v1.hasNext() && v2.hasNext()) { Integer n1 = v1.next(); Integer n2 = v2.next(); if(n1 < n2) { result.add(n1); v1.remove(); v2.add(n2); } else { result.add(n2); v2.remove(); v1.add(n1); } while(v1.hasNext()) { result.add(v1.next()); v1.remove(); } while(v2.hasNext()) { result.add(v2.next()); v2.remove(); } void merge(SimpleList v1, SimpleList v2, SimpleList result) { v1.reset(); v2.reset(); result.reset(); while(v1.hasNext() && v2.hasNext()) { Integer n1 = v1.next(); Integer n2 = v2.next(); if(n1 < n2) { result.add(n1); v1.remove(); v2.add(n2); } else { result.add(n2); v2.remove(); v1.add(n1); } while(v1.hasNext()) { result.add(v1.next()); v1.remove(); } while(v2.hasNext()) { result.add(v2.next()); v2.remove(); } void mergeSort(SimpleList v) { if(v.size() < 2) return; SimpleList v1 = new SimpleList (); SimpleList v2 = new SimpleList (); v.reset(); int half = v.size()/2; while(v.hasNext()) { v1.add(v.next()); v.remove(); if(v.hasNext()) { v2.add(v.next()); v.remove(); } mergeSort(v1); mergeSort(v2); merge(v1, v2, v); } void mergeSort(SimpleList v) { if(v.size() < 2) return; SimpleList v1 = new SimpleList (); SimpleList v2 = new SimpleList (); v.reset(); int half = v.size()/2; while(v.hasNext()) { v1.add(v.next()); v.remove(); if(v.hasNext()) { v2.add(v.next()); v.remove(); } mergeSort(v1); mergeSort(v2); merge(v1, v2, v); }

7 Subclassing and sorting public class SortedList extends SimpleList { public SortedList() { super(); } public boolean add(Integer v) { int smaller = firstItemSmaller(v); if(smaller != null) { remove(); super.add(v); super.add(smaller); } else { super.add(v); } return true; } private Integer firstItemSmaller(Integer v) { reset(); while(hasNext()) { Integer val = next(); if(val < v) return val; } return null; } public class SortedList extends SimpleList { public SortedList() { super(); } public boolean add(Integer v) { int smaller = firstItemSmaller(v); if(smaller != null) { remove(); super.add(v); super.add(smaller); } else { super.add(v); } return true; } private Integer firstItemSmaller(Integer v) { reset(); while(hasNext()) { Integer val = next(); if(val < v) return val; } return null; } SordedList list = new SortedList(); for(int i=0; i<10; i++) { int random = (int)(Math.random() * 100); list.add(random); } SordedList list = new SortedList(); for(int i=0; i<10; i++) { int random = (int)(Math.random() * 100); list.add(random); }

8 Consider searching a sorted array  How to best ‘find’ an item in the array? public int indexOf(int[] data, int val) { for(int i=0; i<data.length; i++){ if(data[i] == val) return i; } return -1; } public int indexOf(int[] data, int val) { for(int i=0; i<data.length; i++){ if(data[i] == val) return i; } return -1; } public int indexOf(int[] data, int val) { int min = 0; int max = data.length-1; while(min <= max) { int mid = (min + max)/2; if(data[mid] == val) return mid; else if(data[mid] < val) min = mid; else max = mid; } return -1; } public int indexOf(int[] data, int val) { int min = 0; int max = data.length-1; while(min <= max) { int mid = (min + max)/2; if(data[mid] == val) return mid; else if(data[mid] < val) min = mid; else max = mid; } return -1; }

9 Insertion Sort public void insertionSort (int[] x) { for (int i=1; i<x.length; i++) { int currentValue = x[i]; int j = i-1; while (j>=0 && x[j]>currentValue) { x[j+1] = x[j]; j--; } x[j+1] = currentValue; }

10 Summary  Bubble Sort  Never useful! Don’t ever write another bubble sort routine!  Selection Sort  Somewhat slower than insertion sort. Don’t use it!  Insertion Sort  Very good when an array is “almost sorted” already  Should be used in some applications and may beat quicksort!  Quick Sort  The best general purpose sorting routine. Use it!  Merge Sort  The best way to sort lists or vectors.  Generic Sorting  Use the Comparable and/or Comparator interfaces


Download ppt "Sorting Putting things in order. Searching and Sorting  Searching and sorting are two of the most used and most studied types of algorithms in computing."

Similar presentations


Ads by Google