100 likes | 354 Views
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 In the early 70’s approximately 80% of computer usage was spent either sorting data or searching data Examples :
E N D
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 • 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
Searching and Sorting • Many methods for searching/sorting; which 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
Selection Sort public void selectionSort (int[] data) { for(inti=0; i<data.length; i++) { int min = indexOfMinimum(data, i); int temp = data[min]; data[min] = data[i]; data[i] = data[temp]; } } public intindexOfMinimum(int[] vals, inti) { 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);
Selection SortCan we modify to sort arrays of objects? public void selectionSort (int[] data) { for(inti=0; i<data.length; i++) { int min = indexOfMinimum(data, i); int temp = data[min]; data[min] = data[i]; data[i] = data[temp]; } } public intindexOfMinimum(int[] vals, inti) { 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);
Consider • How to sort a simple list? • Can we use the ‘array=based’ selection sort? void mergeSort(SimpleList<Integer> v) { if(v.size() < 2) return; SimpleList v1 = new SimpleList<Integer>(); SimpleList v2 = new SimpleList<Integer>(); 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 merge(SimpleList<Integer> v1, SimpleList<Integer> v2, SimpleList<Integer> 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(); } }
Subclassing and sorting public class SortedListextends SimpleList<Integer> { 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(inti=0; i<10; i++) { int random = (int)(Math.random() * 100); list.add(random); }
Consider searching a sorted array • How to best ‘find’ an item in the array? public intindexOf(int[] data, intval) { for(inti=0; i<data.length; i++){ if(data[i] == val) return i; } return -1; } • public intindexOf(int[] data, intval) { • 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; • }
Insertion Sort public void insertionSort (int[] x) { for (inti=1; i<x.length; i++) { intcurrentValue= x[i]; int j = i-1; while (j>=0 && x[j]>currentValue) { x[j+1] = x[j]; j--; } x[j+1] = currentValue; } }
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 goodwhen 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