Download presentation
Presentation is loading. Please wait.
Published byΚυρία Σπυρόπουλος Modified over 5 years ago
1
CIS265/506 Simple Sorting CIS265/506: Chapter 03 - Sorting
2
CIS265/506: Chapter 03 - Sorting
Why do we need sorting? Sorting is the act of putting a set of data in sequence (or in some order). Doing some processing actions on sorted records is more efficient (faster!) than on non-sorted records. INTERNAL SORTING - the number of records are small enough that all the set can be stored completely in main memory. EXTERNAL SORTING - some records need to be placed in auxiliary storage. CIS265/506: Chapter 03 - Sorting CIS265/506: Chapter 03 - Sorting 10
3
CIS265/506: Chapter 03 - Sorting
More Terminology Sort Order - The sequence of the sorted data, which is either ascending (smallest to largest, A to Z) or descending (largest to smallest, Z to A) Sort Stability - Data with equal keys maintain relative order input order in the output. Stable: guaranteed to be in the same order as the input. Unstable: can occur in any order CIS265/506: Chapter 03 - Sorting
4
CIS265/506: Chapter 03 - Sorting
Sort Stability 365 blue 212 green 876 white 212 yellow 119 purple 737 green 212 blue 443 red 567 yellow 119 purple 212 green 212 yellow 212 blue 365 blue 443 red 567 yellow 737 green 876 white 119 purple 212 blue 212 green 212 yellow 365 blue 443 red 567 yellow 737 green 876 white Unsorted Data Stable Sort Unstable Sort CIS265/506: Chapter 03 - Sorting
5
CIS265/506: Chapter 03 - Sorting
Terminology Sort Efficiency (Big O) - Relative efficiency of a sort, or in the general case, relative efficiency of any algorithm Pass - Each traversal of the data. May traverse the entire list, or just a portion. Each traversal should result in the placement of an element in the sorted list. CIS265/506: Chapter 03 - Sorting
6
CIS265/506: Chapter 03 - Sorting
The simplest and most straightforward sorting methods have a moderate growth rate of O(N2). If you have a small data set, and only need to sort it occasionally, these simple sorts are easier to use and are worth the trade off. CIS265/506: Chapter 03 - Sorting CIS265/506: Chapter 03 - Sorting 11
7
CIS265/506: Chapter 03 - Sorting
In Place refers to a sort that uses almost no extra memory than needed by the original data itself In Order refers to a sort that keeps equal valued keys in the same order they initially appeared in CIS265/506: Chapter 03 - Sorting
8
Analysis if the data are in some order already
Best Case, Average Case and Worst Case may or may not be different for a particular sort algorithm. For example, a quick sort is O(N log N) on average, but if data is almost sorted already, it is O(N2). The basic sorts in this chapter do not have different cases Nearly Sorted Results can allow us to use simplified algorithms. For example, inserting one item into an already sorted array is an O(N) operation, and thus simpler than sorting the entire array. Note, since there are N items to insert the total time is still O(N2) CIS265/506: Chapter 03 - Sorting
9
CIS265/506: Chapter 03 - Sorting
Sorting arrays To sort items in an array, we will repeatly need to swap the out-of-value order with some value that belongs in that spot 2 a[i] . . . a[k] 1 3 temp CIS265/506: Chapter 03 - Sorting
10
CIS265/506: Chapter 03 - Sorting
Sept 3, 1998 Arrays Swapping Elements a[i] and a[k] temp = a[i]; a[i] = a[k]; a[k] = temp; CIS265/506: Chapter 03 - Sorting CIS265/506: Chapter 03 - Sorting 32
11
CIS265/506: Chapter 03 - Sorting
Selection Sort The selection sort is intuitively easy to understand. The basic process: Given an array[0 - N], place the smallest item in the array in space 0, the second smallest in space 1, and so forth. We do this by comparing A[0] with A[1] and swapping if necessary. We continue in this manner until the smallest item appears in A[0]. We then forget about A[0], and repeat the process using A[1] (and so on). Each scan of the partial array is called a pass. O(N2) overall, with O(N2) compares and O(N) swaps CIS265/506: Chapter 03 - Sorting CIS265/506: Chapter 03 - Sorting 12
12
CIS265/506: Chapter 03 - Sorting
Selection Sort Swap these 2 Original List unsorted Swap these 2 After Pass One unsorted CIS265/506: Chapter 03 - Sorting
13
CIS265/506: Chapter 03 - Sorting
Selection Sort Swap these 2 After Pass Two unsorted Swap these 2 After Pass Three unsorted CIS265/506: Chapter 03 - Sorting
14
CIS265/506: Chapter 03 - Sorting
Selection Sort Swap these 2 After Pass Four unsorted After Pass Five CIS265/506: Chapter 03 - Sorting
15
CIS265/506: Chapter 03 - Sorting
Selection Sort Initial Array First Pass Second Pass Third Pass CIS265/506: Chapter 03 - Sorting CIS265/506: Chapter 03 - Sorting 13
16
CIS265/506: Chapter 03 - Sorting
Selection Sort void SelectionSort(int data[], int arraySize) { int i, j; for(i = 0; i < (arraySize - 1); i++) { for(j = i + 1; j < arraySize; j++) { if(data[j] < data[i]) swap(data, j, i ); } } } CIS265/506: Chapter 03 - Sorting CIS265/506: Chapter 03 - Sorting 14
17
CIS265/506: Chapter 03 - Sorting
Bubble Sort In a bubble sort, we compare adjacent items, exchanging them if necessary. At the end of the first pass, the “heaviest” (largest) item will have “sunk” to the bottom. The second pass will start at A[0] and go to A[N-1], the third pass will start at A[0] and continue to A[N-2] and so forth. When we make a complete pass where no items are exchanged, the sort is complete. O(N2) overall, with O(N2) compares and O(N2) swaps CIS265/506: Chapter 03 - Sorting CIS265/506: Chapter 03 - Sorting 15
18
CIS265/506: Chapter 03 - Sorting
Bubble Sort Initial Array First Pass Third Pass Second Pass CIS265/506: Chapter 03 - Sorting CIS265/506: Chapter 03 - Sorting 16
19
CIS265/506: Chapter 03 - Sorting
Bubble Sort Routine public static void myBubble1(int[] myArray) { int i, j; for(i = 0; i < myArray.length - 1; i++) { for (j = myArray.length - 1; j > i; --j) { if (myArray[j] < myArray[j-1]) { swap(a, j, j-1); } CIS265/506: Chapter 03 - Sorting CIS265/506: Chapter 03 - Sorting 17
20
CIS265/506: Chapter 03 - Sorting
Bubble Sort Routine public static void myBubble2(int[] myArray) { int i, j, temp; for(i = 0; i < myArray.length - 1; i++) { for (j = 0; j < myArray.length - 1; j++) { if (myArray[j] > myArray[j+1]) { swap(a, j, j+1); } CIS265/506: Chapter 03 - Sorting CIS265/506: Chapter 03 - Sorting 17
21
CIS265/506: Chapter 03 - Sorting
public static void main(String[] args) { int[] theArray = {25,57,48,37,12,92,86,33}; int[] theArray2 = {25,57,48,37,12,92,86,33}; for (int i = 0; i < theArray.length; i++) { System.out.print(theArray[i] + "\t"); } myBubble1(theArray); myBubble2(theArray2); for (int i = 0; i < theArray2.length; i++) { System.out.print(theArray2[i] + "\t"); System.out.println(" "); CIS265/506: Chapter 03 - Sorting
22
CIS265/506: Chapter 03 - Sorting
CIS265/506: Chapter 03 - Sorting
23
CIS265/506: Chapter 03 - Sorting
Bubble Sort To “save” execution time, an alternate procedure called a shaker sort is sometimes used. The passes are alternated so that every other pass moves the lightest or lowest item to the top of the array. CIS265/506: Chapter 03 - Sorting CIS265/506: Chapter 03 - Sorting 18
24
Shaker Sort public class ShakerSort {
public static void shakerSort(int [] a) for (int p = 1; p <= a.length / 2; p++) {// first do left to right bubbling pass for (int i = p - 1; i < a.length - p; i++) if (a[i] > a[i+1] ) swap(a, i, i+1); // now do right to left bubbling pass for (int i = a.length - p - 1; i >= p; i--) if (a[i] < a[i-1] ) swap(a, i, i-1); } CIS265/506: Chapter 03 - Sorting 18
25
CIS265/506: Chapter 03 - Sorting
Insertion Sort This method is very similar to what one does in preparing to play a game of cards, where one receives cards one at a time and orders them in the hand. As each new card arrives, the player scans his hand, conceptually left-to-right, searching for the correct place for the new arrival, then inserts the arrival in that place. O(N2) overall, with O(N2) compares and O(N2) exchanges CIS265/506: Chapter 03 - Sorting CIS265/506: Chapter 03 - Sorting 19
26
CIS265/506: Chapter 03 - Sorting
Insertion Sort Assume an N-element array called A already exists, with K < N elements in ascending order already in the first K locations. To place a new arrival: Search sequentially through the array until a key is found which is greater than that of the new arrival. Call its location J. Make space for the new arrival by moving the contents of A(J) through A(K) to A(J+1) to A(K+1). Insert the newly-arrived element at A(J). CIS265/506: Chapter 03 - Sorting CIS265/506: Chapter 03 - Sorting 20
27
CIS265/506: Chapter 03 - Sorting
Insertion Sort Initial Array 37 Inserted 57 Inserted 48 Inserted CIS265/506: Chapter 03 - Sorting CIS265/506: Chapter 03 - Sorting 21
28
CIS265/506: Chapter 03 - Sorting
Insertion Sort void Insertion(int data[], int arraySize) { int i, j, tmp; for (i=1; i < arraySize; i++) { tmp = data[i]; j = i; while(j > 0 && tmp < data[j-1]) { data[j] = data[j-1]; j--; } data[j] = tmp; } } CIS265/506: Chapter 03 - Sorting CIS265/506: Chapter 03 - Sorting 22
29
CIS265/506: Chapter 03 - Sorting
Sorting Objects What *exactly* do we sort? How do we compare objects? >, >=, ==, <, <=, != don’t work as expected An example follows: No generic coding Generic coding We can not use generics in array declarations CIS265/506: Chapter 03 - Sorting
30
CIS265/506: Chapter 03 - Sorting
public class Book { public Book(String title, int pages) { this.title = title; this.pages = pages; } public Book() { this.title = ""; this.pages = 0; …. public int compareTitle(Book tB){ String t1 = this.title.toLowerCase(); String t2 = tB.getTitle().toLowerCase(); return t1.compareTo(t2); private String title; private int pages; CIS265/506: Chapter 03 - Sorting
31
CIS265/506: Chapter 03 - Sorting
public class SortRoutines { public static void myBubble2(Book[] myArray) { int i, j; Book temp; for(i = 0; i < myArray.length - 1; i++) { for (j = 0; j < myArray.length - 1; j++) { if (myArray[j].compareTitle(myArray[j+1]) > 0) { temp = myArray[j]; myArray[j] = myArray[j+1]; myArray[j+1] = temp; } CIS265/506: Chapter 03 - Sorting
32
CIS265/506: Chapter 03 - Sorting
public class SortRoutines { public static void myBubble2(Book[] myArray) { int i, j; Book temp; for(i = 0; i < myArray.length - 1; i++) { for (j = 0; j < myArray.length - 1; j++) { if (myArray[j].compareTitle(myArray[j+1]) > 0) { temp = myArray[j]; myArray[j] = myArray[j+1]; myArray[j+1] = temp; } CIS265/506: Chapter 03 - Sorting
33
CIS265/506: Chapter 03 - Sorting
public int compareTitle(Book tB){ String t1 = this.title.toLowerCase(); String t2 = tB.getTitle().toLowerCase(); return t1.compareTo(t2); } CIS265/506: Chapter 03 - Sorting
34
CIS265/506: Chapter 03 - Sorting
SNIPPET OF MAIN METHOD Book[] toSort = new Book[4]; SortRoutines.myBubble2(toSort); for (int i = 0; i < 4; i++) { System.out.println("Book " + (i+1) + " is " + theBooks[i].getTitle() + " with " + theBooks[i].getPages() + " pages." ); } CIS265/506: Chapter 03 - Sorting
35
CIS265/506: Chapter 03 - Sorting
Output Sorted Array - by title Book 1 is 100 Years of Solitude with 225 pages. Book 2 is Java By Example with 900 pages. Book 3 is The McGuffey Reader with 51 pages. Book 4 is The Shining with 450 pages. CIS265/506: Chapter 03 - Sorting
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.