Download presentation
Presentation is loading. Please wait.
Published byCecilia Lane Modified over 9 years ago
1
CMPS1371 Introduction to Computing for Engineers SORTING
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
Create Insertion Sort It will take in an array of numbers and return a new array with those numbers in numerical order. 246 81012 62 4 81012 Old Array 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? 62 4 81012 Old Array 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? 62 4 81012 6 Old Array New Array
11
Insertion Sort 26 62 4 81012 Old Array New Array
12
Insertion Sort 26 12 62 4 81012 Old Array New Array
13
Insertion Sort 246 12 62 4 81012 Old Array New Array
14
Insertion Sort 246 1012 62 4 81012 Old Array New Array
15
Insertion Sort 246 81012 62 4 81012 Old Array New Array
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 1. We need to determine whether our new number is smaller than a specific number in the array. 2. 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. 3. Otherwise, we need to keep going to find the right place to insert this new number. 4. If we are at the end of our array, we need to add our new number at the end.
18
Inserting in the Middle 246 81210 Say we want to insert 7 here – how would you do it? 1.Make space for it 2.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 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) = n 2 /4 O(n 2 ) - 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] Compare 1 to 3. Insert 1 into new list. Continue across each array until one is empty. [1] Start a new array
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] And continue across each array until one is empty. [1 2]
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] And continue across each array until one is empty. [1 2 3]
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] And continue across each array until one is empty. [1 2 3 4]
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] And continue across each array until one is empty. [1 2 3 4 5]
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] And continue across each array until one is empty. [1 2 3 4 5 6]
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] And continue across each array until one is empty. [1 2 3 4 5 6 19]
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] And continue across each array until one is empty. [1 2 3 4 5 6 19 20]
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] And continue across each array until one is empty. [1 2 3 4 5 6 19 20 27]
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] Then, just copy the rest of the surviving array. [1 2 3 4 5 6 19 20 27 30]
35
mergesort Algorithm 1.The function receives as input an array of numbers 2.If the array contains more than one element, divide the array into two arrays of equal size (plus or minus an element). 3.Perform mergesort on the first array 4.Perform mergesort on the second array 5.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
674523146339842 674523146339842 45231498 23984514 6763342 6763342 239845146764233 14234598 6334267 614233342456798 n 2*log(n) O(n log(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. 1.We arbitrarily choose an element of our array to be a pivot item. 2.We then partition our array into two parts: those elements that are smaller than our pivot those elements that are larger than our pivot 3.We recursively sort the two parts, and join the results together with our pivot item to form a larger, sorted array.
43
674523146333642 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
674523146333642 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: 676143645332342
45
674523146333642 676143645332342 We start recurring on our two arrays. The pivot points for each of our sub-arrays.
46
674523146333642 676143645332342 6143367454223 6 14 3367454223
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.