Presentation is loading. Please wait.

Presentation is loading. Please wait.

Lecture 1 -- 1Computer Science I - Martin Hardwick Searching and sorting rReasons for not using the most efficient algorithm include: l The more efficient.

Similar presentations


Presentation on theme: "Lecture 1 -- 1Computer Science I - Martin Hardwick Searching and sorting rReasons for not using the most efficient algorithm include: l The more efficient."— Presentation transcript:

1 Lecture 1 -- 1Computer Science I - Martin Hardwick Searching and sorting rReasons for not using the most efficient algorithm include: l The more efficient algorithm is much more complicated. Therefore, it is harder to maintain and debug. l The more efficient algorithm requires another part of the system to prepare data and preparing this data takes a long time. rFor example, binary search requires sorted data l As we shall see the sorting algorithms are relatively complicated. l And they are slow – the best algorithms are O (n log n). l Making searching faster by going from O (n) to O (log n) is not worth it if the price is having to use a new algorithm that is O(n log n). rHowever, society has organized itself so that sorting does not have to be used too often l For example, banks only sort their data once per day

2 Lecture 1 -- 2Computer Science I - Martin Hardwick Creating a Sorted List void banking::Insert( /* in */ acct item ) // Inserts into a list of bank accounts // Data is vector { int index = data.size() - 1; data.push_back (item); while (index >= 0 && item.get_num() < data[index].get_num()) { data[index+1] = data[index]; index--; } data[index+1] = item; // Insert item } rAs we have seen, using the Insert() operation for the SortedList class repeatedly allows us to create a sorted list. l but is this efficient? rIn the worst case, each call to Insert() moves the entire list one slot. l first call moves 0 items l second call moves 1 item l third call moves 2 items l... l Nth call moves N-1 items. rFor a list of length N, this requires: l 0 + 1 + 2 + … + (n-1) moves l =

3 Lecture 1 -- 3Computer Science I - Martin Hardwick Discussion rRepeated use of the Insert() operation sorts a list using O(n 2 ) moves and O(n 2 ) comparisons. l each move is preceded by a comparison between variable item and the list element to be moved rWe can do better than repeated use of the Insert() operation, however. rThere is a simple sorting algorithm that uses O(n 2 ) comparisons but only O(n) moves. l it is called Selection Sort

4 Lecture 1 -- 4Computer Science I - Martin Hardwick Selection Sort (1) #include using namespace std; void banking::selection(vector &list) //PURPOSE: sort list into ascending // order using selection sort //POSTCONDITIONS: reorders list // into ascending order { // size of list int length = list.size(); // position of min number int minLoc; // top of sorted part of list int top; rGiven a list of numbers, we want to sort the list into ascending order. rWe will do the sorting in a function that can be moved to any program that needs to sort a list.

5 Lecture 1 -- 5Computer Science I - Martin Hardwick Selection Sort Algorithm rAt any point during the algorithm, the first part of the list is sorted and the last part is unsorted. l initially the first part is empty and the last part is the entire list rSearch the unsorted part of the list for the smallest number. rSwap this smallest number to beginning of unsorted part of the list. rRepeat this process until the list is sorted. 1 2 3 4 20 10 19 9 18 8 17 7 16 6 15 5 14 13 12 sortedunsorted k minLoc k k k k k k k k k k k k k k temp top 520 k minLoc 20 sortedunsorted

6 Lecture 1 -- 6Computer Science I - Martin Hardwick Selection Sort (2) // swap numbers in list int temp; // loop variable intk; // loop to sort list for (top=0; top<=length-2; top++) { // find smallest number in unsorted // part of the list minLoc = top; for (k=top+1; k<=length-1; k++) { if (list[k] < list[minLoc]) { minLoc = k; } rIn a list of length n, once n-1 numbers are in their final positions, the list is sorted. l by default, the remaining number must be in the correct position rStart by assuming that the first number in the unsorted part of the list is the smallest remaining number. l search the rest of the unsorted list for something smaller l remember where the smallest number is

7 Lecture 1 -- 7Computer Science I - Martin Hardwick Selection Sort (3) // swap smallest number // into its final position in // the sorted list temp = list[top]; list[top] = list[minLoc]; list[minLoc] = temp; } rThe program swaps two elements of the list. l the smallest remaining number and the first number in the unsorted part of the list rSwapping requires an extra variable (named temp in this case) to temporarily hold the value of one of the variables being swapped. rThe sorted part of the list is now one element longer. l the unsorted part is one element shorter

8 Lecture 1 -- 8Computer Science I - Martin Hardwick Selection Sort (3) int main () //PURPOSE: test selection function //PRECONDITIONS: file list.txt in // project folder //POSTCONDITIONS: rets 0 if success { vector list; int k; // loop variable int tmp; // tmp variable ifstream listfile; // file with list // read the numbers into the list // and display them on the screen listfile.open("list.txt"); listfile >> tmp; list.push_back (tmp); cout << "Original List:" << endl; while (!listfile.eof()) { cout << tmp << "\t"; listfile >> tmp; list.push_back (tmp); } rRead the list of numbers from a file named list.txt. rDisplay the list before sorting.

9 Lecture 1 -- 9Computer Science I - Martin Hardwick Selection Sort (4) // sort the list selection(list); // display the sorted list cout << endl << endl << "Sorted List:" << endl; for (k=0; k<list.size(); k++) { cout << list[k] << "\t"; } cout << endl << endl; return 0; } rFunction selection() sorts the list. l remember that a vector must be passed by reference rAfter sorting, the list is displayed again so that the new sorted order can be seen.

10 Lecture 1 -- 10Computer Science I - Martin Hardwick Program Results

11 Lecture 1 -- 11Computer Science I - Martin Hardwick Performance Of Selection Sort rThe selection sort function has nested for loops as follows: rThe outer loop is executed n-1 times, where n is the length of the list. rThe inner loop executes: n-1, n-2, n-3,..., 2, 1 times on successive iterations of the outer loop, respectively. l this is an average of about n/2 times for large n rThus, the comparison inside the inner loop is made about (n-1)x(n/2) = 1/2(n 2 -n) times. rTherefore, selection sort is an O(n 2 ) algorithm because of the number of comparisons that it does. rHowever, it does O(n) moves, making it more efficient that repeated calls to the Insert() operator for the SortedList class. for (top=0; top<=length-2; top++) {... for (k=top+1; k<=length-1; k++) { comparison of list elements }

12 Lecture 1 -- 12Computer Science I - Martin Hardwick What Does This Mean? rThe most efficient sorting algorithms can be shown to be O(nlog 2 n), where n is the size of the list to sort. rAs can be seen in the graph O(nlog 2 n) is much better than O(n 2 ) when n gets large. n 2 nlog 2 n n

13 Lecture 1 -- 13Computer Science I - Martin Hardwick More efficient sorting rThe best sorting algorithms are O (n log n) rThese algorithms require a programming technique called recursion rA recursive function is one that calls itself rA recursive function must have at least two parts l A part that solves a simple case of the problem without recursion l A part that makes the problem simpler and then uses recursion rHere is a simple recursive function rMore on recursion next time int factorial (int val) { if (val == 1)// simple part return 1; else// simplification part return (val * factorial (val-1)); }


Download ppt "Lecture 1 -- 1Computer Science I - Martin Hardwick Searching and sorting rReasons for not using the most efficient algorithm include: l The more efficient."

Similar presentations


Ads by Google