Presentation is loading. Please wait.

Presentation is loading. Please wait.

ITEC 2620M Introduction to Data Structures Instructor: Prof. Z. Yang Course Website: 20m.htm Office: TEL 3049.

Similar presentations


Presentation on theme: "ITEC 2620M Introduction to Data Structures Instructor: Prof. Z. Yang Course Website: 20m.htm Office: TEL 3049."— Presentation transcript:

1 ITEC 2620M Introduction to Data Structures Instructor: Prof. Z. Yang Course Website: http://people.math.yorku.ca/~zyang/itec26 20m.htm Office: TEL 3049

2 Sorting

3 3 Key Points of this Lecture Non-recursive sorting algorithms –Selection Sort –Insertion Sort –Best, Average, and Worst cases

4 4 Sorting Why is sorting important? –Easier to search sorted data sets –Searching and sorting are primary problems of computer science Sorting in General –Arrange a set of elements by their “keys” in increasing/decreasing order Example: How would we sort a deck of cards?

5 5 Selection Sort Find the smallest unsorted value, and move it into position What do you do with what was previously there? –“swap” it to where the smallest value was –sorting can work on the original array Example

6 6 Pseudocode Loop through all elements (have to put n elements into place) –for loop Loop through all remaining elements and find smallest –initialization, for loop, and branch Swap smallest element into correct place –single method

7 7 Insertion Sort Get the next value, and push it over until it is “semi” sorted –elements in selection sort are in their final position –elements in insertion sort can still move which do you use to organize a pile of paper? Example

8 8 Pseudocode Loop through all elements (have to put n elements into place) –for loop Loop through all sorted elements, and swap until slot is found –while loop –swap method

9 9 Bubble Sort Slow and unintuitive…useless relay race –pair-wise swaps until smaller value is found –smaller value is then swapped up

10 10 Cost of Sorting Selection Sort –Is there a best, worst, and average case? two for loops  always the same –n elements in outer loop –n-1, n-2, n-3, …, 2, 1 elements in inner loop average n/2 elements for each pass of the outer loop –n * n/2 compares

11 11 Cost of Sorting (Cont’d) Insertion Sort –Worst – same as selection sort, next element swaps till end n * n/2 compares –Best – next element is already sorted, no swaps n * 1 compares –Average – linear search, 50% of values  n/4 n * n/4 compares

12 12 Value of Sorting Current cost of sorting is roughly n 2 compares Toronto phone book –2 million records –4 trillion compares to sort Linear search –1 million compares Binary search –20 compares

13 13 Trade-Offs Write a method, or re-implement each time? Buy a parking pass, or pay cash each time? Sort in advance, or do linear search each time? Trade-offs are an important part of program design –which component should you optimize? –is the cost of optimization worth the savings?


Download ppt "ITEC 2620M Introduction to Data Structures Instructor: Prof. Z. Yang Course Website: 20m.htm Office: TEL 3049."

Similar presentations


Ads by Google