1 / 46

CMPS1371 Introduction to Computing for Engineers

Learn about different sorting algorithms such as bubble sort, selection sort, insertion sort, merge sort, and quick sort. Understand their efficiency, implementation, and applications.

jheller
Download Presentation

CMPS1371 Introduction to Computing for Engineers

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. SORTING CMPS1371Introduction to Computing for Engineers

  2. Sorting Algorithms • A sorting algorithm is an algorithm that puts elements of a list in a certain order. The most used orders are numerical order and alphabetical order. • Efficient sorting is important to optimizing the use of other algorithms that require sorted lists to work correctly.

  3. Sorting Algorithms • Bubble sort • Selection sort • Insertion sort • Merge sort • Quick sort

  4. Bubble Sort • The basic idea is to compare two neighboring objects, and to swap them if they are in the wrong order. • This is probably the simplest way to sort an array of objects. • Unfortunately, it is also the slowest way!

  5. Selection Sort • The idea of selection sort is rather simple. • We repeatedly find the next largest (or smallest) element in the array and move it to its final position in the sorted array. Assume that we wish to sort the array in increasing order, i.e. the smallest element at the beginning of the array and the largest element at the end.

  6. Selection Sort • We begin by selecting the largest element and moving it to the highest index position. We can do this by swapping the element at the highest index and the largest element. We then reduce the effective size of the array by one element and repeat the process on the smaller (sub)array. The process stops when the effective size of the array becomes 1 (an array of 1 element is already sorted).

  7. Insertion Sort • Insertion sort keeps making the left side of the array sorted until the whole array is sorted. It sorts the values seen so far and repeatedly inserts unseen values in the array into the left sorted array.

  8. 2 4 6 8 10 12 Create Insertion Sort It will take in an array of numbers and return a new array with those numbers in numerical order. Old Array 4 6 2 12 10 8 New Array

  9. Insertion Sort Why don’t we start with an empty result array, iterate through the original array, inserting each number one at a time into the new array? Old Array 4 6 2 12 10 8 New Array

  10. Insertion Sort Why don’t we start with an empty result array, iterate through the original array, inserting each number one at a time into the new array? Old Array 4 6 2 12 10 8 New Array 6

  11. Insertion Sort Old Array 4 6 2 12 10 8 New Array 2 6

  12. Insertion Sort Old Array 4 6 2 12 10 8 New Array 2 6 12

  13. Insertion Sort Old Array 4 6 2 12 10 8 New Array 2 4 6 12

  14. Insertion Sort Old Array 4 6 2 12 10 8 New Array 2 4 6 10 12

  15. Insertion Sort Old Array 4 6 2 12 10 8 New Array 2 4 6 8 10 12

  16. Insertion Algorithm function b = insertionsort(a) % This function sorts a column array, using % the insertion sort algorithm b = []; i = 1; size = length(a); while i <= size b = insert(b, a(i) ); % a “helper function” i = i + 1; end

  17. Steps for Insertion • We need to determine whether our new number is smaller than a specific number in the array. • If the new number is smaller than the number in the array, we need to insert the new number now so it will be in front of that bigger number. • Otherwise, we need to keep going to find the right place to insert this new number. • If we are at the end of our array, we need to add our new number at the end.

  18. Inserting in the Middle 2 4 6 8 10 12 Say we want to insert 7 here – how would you do it? • Make space for it • Insert it.

  19. The Insertion Function function a = insert(a, v) % this function inserts the value v % into the array a i = 1; size = length(a); done = 0; while i <= sz %% code to insert in the middle end if done == 0 a(sz+1) = v; end Extends the length of a by 1

  20. Insertion Function while i <= sz if v < a(i) done = 1; j = sz + 1; while j > i a(j) = a(j-1); j = j - 1; end a(i) = v; i = sz + 1; else i = i + 1; end end Count backwards from the end of the array Move each item forwards Insert v before the number at index i

  21. Efficiency • How long would it take: • For each number (there are n) of them • Check on average half of the ones you've already done • On average there are n/2 of them already done • O(insertion sort) = n*(1/2)*(n/2) = n2/4 • O(n2) - don't care about the constant

  22. Better Approach Divide and Conquer: cuts the problem in half each time, but uses the result of both halves: • cut the problem in half until the problem is trivial • solve for both halves • combine the solutions Merge Sort - recursive sorting procedure

  23. Merge Sort • Merge sort works on the premise that combining two sorted arrays is fairly fast. • Consider merging: [1 2 5 19 27] [3 4 6 20 30]

  24. Merge Sort Merge sort works on the premise that combining two sorted lists is fairly fast. Consider merging: [1 2 5 19 27] [3 4 6 20 30] We can compare the first item in each array, which we know to be the smallest from each. If we compare the two, we now know which is the smallest of BOTH lists, and we can save that in the result array.

  25. Merge Sort Merge sort works on the premise that combining two sorted lists is fairly fast. Consider merging: [1 2 5 19 27] [3 4 6 20 30] Start a new array [1] Compare 1 to 3. Insert 1 into new list. Continue across each array until one is empty.

  26. Merge Sort Merge sort works on the premise that combining two sorted lists is fairly fast. Consider merging: [1 2 5 19 27] [3 4 6 20 30] [1 2] And continue across each array until one is empty.

  27. Merge Sort Merge sort works on the premise that combining two sorted lists is fairly fast. Consider merging: [1 2 5 19 27] [3 4 6 20 30] [1 2 3] And continue across each array until one is empty.

  28. Merge Sort Merge sort works on the premise that combining two sorted lists is fairly fast. Consider merging: [1 2 5 19 27] [3 4 6 20 30] [1 2 3 4] And continue across each array until one is empty.

  29. Merge Sort Merge sort works on the premise that combining two sorted lists is fairly fast. Consider merging: [1 2 5 19 27] [3 4 6 20 30] [1 2 3 4 5] And continue across each array until one is empty.

  30. Merge Sort Merge sort works on the premise that combining two sorted lists is fairly fast. Consider merging: [1 2 5 19 27] [3 4 6 20 30] [1 2 3 4 5 6] And continue across each array until one is empty.

  31. Merge Sort Merge sort works on the premise that combining two sorted lists is fairly fast. Consider merging: [1 2 5 19 27] [3 4 6 20 30] [1 2 3 4 5 6 19] And continue across each array until one is empty.

  32. Merge Sort Merge sort works on the premise that combining two sorted lists is fairly fast. Consider merging: [1 2 5 19 27] [3 4 6 20 30] [1 2 3 4 5 6 19 20] And continue across each array until one is empty.

  33. Merge Sort Merge sort works on the premise that combining two sorted lists is fairly fast. Consider merging: [1 2 5 19 27] [3 4 6 20 30] [1 2 3 4 5 6 19 20 27] And continue across each array until one is empty.

  34. Merge Sort Merge sort works on the premise that combining two sorted lists is fairly fast. Consider merging: [1 2 5 19 27] [3 4 6 20 30] [1 2 3 4 5 6 19 20 27 30] Then, just copy the rest of the surviving array.

  35. mergesort Algorithm • The function receives as input an array of numbers • If the array contains more than one element, divide the array into two arrays of equal size (plus or minus an element). • Perform mergesort on the first array • Perform mergesort on the second array • Merge the results of the two recursive calls together

  36. mergesort function a = mergesort(a) % This function sorts a column array, using the % merge sort algorithm size = length(a); if sz > 1 szb2 = floor(sz/2); first = a(1:szb2); second = a(szb2+1:sz); first = mergesort(first); second = mergesort(second); b = domerge(first, second); end

  37. mergesort function b = domerge(first, second) % Merges two sorted arrays into the array to be % sorted by this merge sorter. iFirst = 1; % first array index iSecond = 1; % second array index out = 1; % out array index sf = size(first); ss = size(second); continued

  38. mergesort while (iFirst <= sf) & (iSecond <= ss) if first(iFirst) < second(iSecond) b(out) = first(iFirst); iFirst = iFirst + 1; else b(out) = second(iSecond); iSecond = iSecond + 1; end out = out + 1; end continued

  39. mergesort % note that only one of the two while loops % below is executed % copy any remaining entries of the first array while iFirst <= sf(1) b(out) = first(iFirst); out = out + 1; iFirst = iFirst + 1; end % copy any remaining entries of the second array while iSecond <= ss(1) b(out) = second(iSecond); out = out + 1; iSecond = iSecond + 1; end

  40. 98 23 45 14 6 67 33 42 2*log(n) 98 23 45 14 6 67 33 42 98 23 45 14 6 67 33 42 98 23 45 14 6 67 33 42 23 98 14 45 6 67 33 42 6 33 42 67 14 23 45 98 6 14 23 33 42 45 67 98 O(n log(n)) n

  41. Quick Sort Quick sort is another generative “divide and conquer” algorithm that involves splitting our sequence of data into parts and sorting each component recursively. Unlike merge sort, however, quick sort performs the actual sorting as the sequence is being split. In terms of performance, quick sort is considered to be a “better” algorithm than merge sort unless the original array was already sorted!

  42. Quick Sort: The basic premise The basic idea of quick sort involves splitting up our array around a pivot item. • We arbitrarily choose an element of our array to be a pivot item. • We then partition our array into two parts: • those elements that are smaller than our pivot • those elements that are larger than our pivot • We recursively sort the two parts, and join the results together with our pivot item to form a larger, sorted array.

  43. 36 23 45 14 6 67 33 42 Here we start off with an array of unsorted elements. We arbitrarily choose a pivot point about which to organize our list. For the sake of expediency, let’s just choose the first element of our list to be our pivot.

  44. 36 23 45 14 6 67 33 42 Now, we have to organize our array about our pivot point. We separate the list into three parts: • The pivot value • An array of items smaller than the pivot value • An array of items larger than the pivot value: 23 14 6 33 36 45 67 42

  45. 36 23 45 14 6 67 33 42 23 14 6 33 36 45 67 42 The pivot points for each of our sub-arrays. We start recurring on our two arrays.

  46. 36 23 45 14 6 67 33 42 23 14 6 33 36 45 67 42 14 6 23 33 42 45 67 14 6 23 33 42 45 67

More Related