130 likes | 275 Views
Sorting. Summary of Sorting Algorithms. _Plan. General idea of sorting: we start with an array of items in no particular order, and we want to convert it into a sorted array in either ascending or descending order. Definition: Selection Sort (array of double numbers).
E N D
Sorting Summary of Sorting Algorithms
_Plan • General idea of sorting: we start with an array of items in no particular order, and we want to convert it into a sorted array in either ascending or descending order.
Definition: Selection Sort (array of double numbers) • Find the smallest element in the entire array A and swap it with the first element--now the first element is in the correct position • Find the smallest element in A minus the first element, and swap it with the second element--now the first two elements are in the correct order. • Find the smallest element in A disregarding the first two elements, and swap it with the third element. Now the first three elements are in the correct order. Continue in this fashion. • This algorithm is illustrated by the following figure: • Figure: How Selection Sort works • public static boolean search(double A[], double key) • { for (int i = 0; i < A.length; i++) • if (A[i] == key) • return true; • return false; • }
Search which returns the object esired 3 • 22 is the smallest overall number and is swapped with the first number • 27 is the smallest number starting at the second one, and swapped with itself • 45 is the smallest number starting at the third, and swapped with the third
Definition: Bubble Sort (Exchange Sort) • Process the full array by comparing consecutive elements: if the (i+1)st element is less than the ith element, swap them with each other, otherwise leave them alone. • If no exchange is made, the array is sorted and we are done. • At the end of this pass the overall largest number will have "bubbled" to the last position of the array. • Repeat this "bubbling" procedure, but apply it to the current array except the last element.
Definition: Bubble Sort (Exchange Sort) • If no exchange was made, the array is sorted and we are done. At the end of this pass the second-largest number will have "bubbled" to the second-last position of the array. • Repeat this "bubbling" procedure, but apply it to the current array except the last two elements. If no exchange was made, the array is sorted and we are done. • Continue in this fashion until either no exchange was necessary during a particular pass, or there is nothing left to sort.
Definition: Insertion Sort • Consider the second last element in the array. If the last element is less than this one, move it up by one and insert the second-last element in the last position. • Consider the third-last element in the array, and the last two elements of the array. Move all entries in that subarray that are less than the third-last element up by one, and insert the third-last element into the resulting empty slot.
Implementation • if (userArray[n] < userArray[counter]) • { • // Swap the numbers in the array • temp = userArray[n]; • userArray[n] = userArray[counter]; • userArray[counter] = temp; • }
Demonstrate • Goto SelectBinary.java • http://sciris.shu.edu/thinklets--------go to COmpSci---------Sorting
Code • /* • Author(s): Felicia Escorpizo, Chunkai Szu, Rehan Malik • Date: June 13, 2000 • Program: SelectBinary.java • Description: Get and sort array according to selection sort algorithm and perform binary search • Pre-condition/Post-Condition: Get the values from the user to input into the array. • Print the original array and sorted array. • Print the value in the array to search for (if selectec). • Design Roles: Initialize array and fill with values • Initialize a few counter int values • Design Idea: • 1. Get the array from the user and input into an array. • 2. Print the original array. • 3. Run selection sort algorithm • a. Select first value and compare against the rest of the values. (switch if need) • b. Select second value and compare against the rest of the values. (switch if need). • c. Continue until end of array. • 4. Array is now sorted. • 5. Ask user if he/she wants to search for a value. • 6. Perform binary search and print results. • */ • import java.io.*; • public class SelectBinary • { • public static void main(String args[]) • { • // Initialize a few variables • int sizeArray, smallest, temp, a, b, i, n, counter, key; • // Get the size of the array • System.out.print("Enter the size of the desired array: "); • sizeArray = Console.readInt(); • int userArray[] = new int[sizeArray]; • // Get values to input into the array • for (i = 0; i < sizeArray; i++) • { • System.out.print("Enter value number " + (i+1) + " of the array: "); • userArray[i] = Console.readInt(); • if (userArray[i] < 0) • { • System.out.println("User input error...values may not be negative...exiting...\n"); • System.exit(0); • } • } • System.out.println("\nOriginal Array is: "); • for (a = 0; a < sizeArray; a++) • { • System.out.print(userArray[a] + " "); • } • System.out.println("\n"); • // Run the selection sort algorithm • for (n = 0; n < sizeArray; n++) • { • for (counter = 0; counter < sizeArray; counter++) • { • if (userArray[n] < userArray[counter]) • { • // Swap the numbers in the array • temp = userArray[n]; • userArray[n] = userArray[counter]; • userArray[counter] = temp; • } • } // End the 2nd internal for loop • } // End the 1st for loop • // Print the sorted array to the screen • System.out.println("\nThe Sorted Array is: "); • for (b = 0; b < sizeArray; b++) • { • System.out.print(userArray[b] + " "); • } • System.out.println("\n"); • System.out.print("Would you like to search for a value from the array? (1 for Yes - 2 for No) "); • int searchBin = Console.readInt(); • // Perform Binary Search on the array • if (searchBin == 1) • { • System.out.println("Enter the key to search for: "); • key = Console.readInt(); • int low = 0; • int high = userArray.length - 1; • int middle; • while (low <= high) • { • middle = (low + high) / 2; • if (key == userArray[middle]) • { • // Key was found so print message • System.out.println("Key was found at"+ (middle+1)+"position" ); • System.exit(0); • } • else if (key < userArray[middle]) • high = middle - 1; • else • low = middle + 1; • } • // Key was not found and at end of program...so exiting... • System.out.println("Key was not found...end of program...\n"); • } • } • }