Sorting Summary of Sorting Algorithms Plan General idea










![Implementation • if (user. Array[n] < user. Array[counter]) • { • // Swap the Implementation • if (user. Array[n] < user. Array[counter]) • { • // Swap the](https://slidetodoc.com/presentation_image_h/123a13f2258924ecdcd76374daad65e3/image-11.jpg)


- Slides: 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 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.

Example: (Selection)

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.

Example (Bubble)

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.

Example (Insertion)
![Implementation if user Arrayn user Arraycounter Swap the Implementation • if (user. Array[n] < user. Array[counter]) • { • // Swap the](https://slidetodoc.com/presentation_image_h/123a13f2258924ecdcd76374daad65e3/image-11.jpg)
Implementation • if (user. Array[n] < user. Array[counter]) • { • // Swap the numbers in the array • temp = user. Array[n]; • user. Array[n] = user. Array[counter]; • user. Array[counter] = temp; • }

Demonstrate • Goto Select. Binary. java • http: //sciris. shu. edu/thinklets----go to COmp. Sci-----Sorting

Code • /* • Author(s): Felicia Escorpizo, Chunkai Szu, Rehan Malik • Date: June 13, 2000 • Program: Select. Binary. java • Description: Get and sort array according to selection sort algorithm and perform binary search