sorting summary of sorting algorithms. _plan general idea of sorting: we start with an array of...

13
Sorting Sorting Summary of Sorting Algorithms

Upload: jayson-carroll

Post on 16-Dec-2015

214 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: 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

SortingSorting

Summary of Sorting Algorithms

Page 2: 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

_Plan _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.

Page 3: 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

Definition:Definition: Selection Sort (array Selection Sort (array of double numbers)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;• }

Page 4: 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

Example: (Selection)Example: (Selection)

5 62 72 29 57 94 59 6

2 22 75 69 57 94 59 6

2 22 75 69 57 94 59 6

2 22 74 59 57 95 69 6

2 22 74 5

7 9

9 6

5 6

9 5

2 22 74 55 67 99 59 6

2 22 74 55 67 99 59 6

Page 5: 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

Search which returns the object Search which returns the object esired esired 33

• 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

Page 6: 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

Definition:Definition: Bubble Sort Bubble Sort (Exchange 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.

Page 7: 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

Definition:Definition: Bubble Sort Bubble Sort (Exchange 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.

Page 8: 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

Example (Bubble)Example (Bubble)

5 69 52 72 27 94 59 6

5 69 52 72 27 94 59 6

5 62 79 52 27 94 59 6

5 62 72 29 57 94 59 6

5 62 72 27 99 54 59 6

5 62 72 27 94 59 59 6

5 62 72 27 94 59 596

Page 9: 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

Definition: Insertion Definition: Insertion SortSort• 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.

Page 10: 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

Example (Insertion)Example (Insertion)5 62 72 29 57 94 59 6

5 6

2 22 7

9 57 94 596

5 62 72 29 54 57 996

5 62 72 24 5799596

5 62 72 24 5799596

5 62 22 74 5799596

22274556799596

Page 11: 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

ImplementationImplementation

• if (userArray[n] < userArray[counter])

• {

• // Swap the numbers in the array

• temp = userArray[n];

• userArray[n] = userArray[counter];

• userArray[counter] = temp;

• }

Page 12: 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

DemonstrateDemonstrate

• Goto SelectBinary.java

• http://sciris.shu.edu/thinklets--------go to COmpSci---------Sorting

Page 13: 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

CodeCode

• /*• 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");• }• }• }