Presentation is loading. Please wait.

Presentation is loading. Please wait.

Sorting & Searching Review. Selection Sort 1. Find the smallest element 2. Move to the front of the array (swap with front) 3. Repeat Steps 1&2, but ignoring.

Similar presentations


Presentation on theme: "Sorting & Searching Review. Selection Sort 1. Find the smallest element 2. Move to the front of the array (swap with front) 3. Repeat Steps 1&2, but ignoring."— Presentation transcript:

1 Sorting & Searching Review

2 Selection Sort 1. Find the smallest element 2. Move to the front of the array (swap with front) 3. Repeat Steps 1&2, but ignoring the front

3 Selection Sort Animations http://www.ece.unb.ca/brp/lib/java/selection sort/ http://www.ece.unb.ca/brp/lib/java/selection sort/ http://www.cs.queensu.ca/~cisc121/2004f/l ecturenotes/malamb/SortingDemos/Selecti onSortDemo.html http://www.cs.queensu.ca/~cisc121/2004f/l ecturenotes/malamb/SortingDemos/Selecti onSortDemo.html http://web.engr.oregonstate.edu/~minoura/ cs162/javaProgs/sort/SelectSort.html http://web.engr.oregonstate.edu/~minoura/ cs162/javaProgs/sort/SelectSort.html

4 Selection Sort Example Code int currentMinIndex = 0; for (int front = 0; front < intArray.length; front++) { currentMinIndex = front; for (int i = front; i < intArray.length; i++) { if (intArray[i] < intArray[currentMinIndex]) { currentMinIndex = i; } int tmp = intArray[front]; intArray[front] = intArray[currentMinIndex]; intArray[currentMinIndex] = tmp; }

5 Bubble Sort 1. Start at the bottom / end of the array 2. Compare the two elements and swap them if the smaller number is on the bottom 3. Move up one 4. Repeat steps 1 through 3 until smallest number has "floated" to the top 5. Start at the bottom again and repeat the above steps for the next smallest number. This time, the front / top of the list can be ignored because the smallest number is guaranteed to be there.

6 Bubble Sort Animations http://www.ece.unb.ca/brp/lib/java/bubbleso rt/ http://www.ece.unb.ca/brp/lib/java/bubbleso rt/ http://web.engr.oregonstate.edu/~minoura/ cs162/javaProgs/sort/BubbleSort.html http://web.engr.oregonstate.edu/~minoura/ cs162/javaProgs/sort/BubbleSort.html

7 Bubble Sort Example Code int front = 0; int pos = 0; for (front = 0; front < intArray.length; front++) { for (pos = intArray.length-1; pos > front; pos--) { if (intArray[pos] < intArray[pos-1]) { int tmp = intArray[pos]; intArray[pos] = intArray[pos-1]; intArray[pos-1] = tmp; }

8 Linear Search 1. Start from the beginning of the list 2. Check if current element matches key 3. Move to next element 4. Repeat steps 2-3 until key is found or end of list is reached

9 Linear Search Example Code int key = 0;//key may be any value for(int i = 0; i < intArray.length; i++) { if (key == intArray[i]) return i; } return -1;

10 Binary Search 1. Assume sorted list 2. Go to the middle point 3. If the middle element matches the key, then the search is over 4. If key is less than middle element, go to the left (down), else go to the right (up) 5. Repeat steps 2-4 until key is found or when the left and right bounds pass each other

11 Binary Search Example Code int key = 0;//key may be any value int first = 0; int last = intArray.length-1;; int mid = 0; boolean found = false; while( (!found) && (first <= last) ) { mid = (first + last) / 2; if(key == intArray[mid]) found = true; if(key < intArray[mid]) last = mid - 1; if(key > intArray[mid]) first = mid + 1; }


Download ppt "Sorting & Searching Review. Selection Sort 1. Find the smallest element 2. Move to the front of the array (swap with front) 3. Repeat Steps 1&2, but ignoring."

Similar presentations


Ads by Google