Download presentation
Presentation is loading. Please wait.
Published byCharles Miller Modified over 8 years ago
1
Sorting – Part I CS 367 – Introduction to Data Structures
2
Searching We have already seen many methods to search for data –brute force –binary search –hashing –sorted binary trees Most searching algorithms rely on data to be in some order –especially for arrays
3
Sorting Arrays It is common to insert data into an array out of order –imagine reading a line of text from a file –inserting students grades To make searching of this data more efficient, it needs to be sorted –if you are to do a lot of searching, the expense to do a sort is easily paid back
4
Insertion Sort Basic procedure –start at array element 1 –if an element is smaller than any of the elements proceeding it, shift all the elements larger than element to the right by 1 –insert the element into blank space
5
Insertion Sort 0123456 cbefdga 0123456 bcefdga 0123456 bcdefga 0123456 abcdefg insert b at position 0 insert d at position 2 insert a at postion 0
6
Insertion Sort Code public void insertionSort(Object[ ] data) { Comparable tmp; int i, k; for(i = 1; i < data.length; i++) { tmp = Object[i]; for(k = i; (k>0) && tmp.compareTo(data[k-1]) < 0; k--) data[k] = data[k-1]; data[i] = tmp; }
7
Selection Sort Basic procedure –find the smallest element in the array swap it with the element at index 0 –find the next smallest element swap it with the element at index 1 –continue this until all the elements are in the right spot
8
Selection Sort 0123456 cbefdga 0123456 abefdgc 0123456 abcfdge 0123456 abcdfge swap a with c swap c with e swap d with f swap e with f 0123456 abcdegf 0123456 abcdefg swap f with g
9
Selection Sort Code public void selectionSort(Object[ ] data) { int i, k, least; for(i=0; i<data.length-1; i++) { least = i; for(k = i+1; k<data.length; k++) { if(((Comparable)data[k]).compareTo(data[least]) < 0) least = k; } swap(i, least); }
10
Bubble Sort Basic procedure –start at the back of an array –compare last element with second to last swap them if last element is smaller –compare second to last with third to last swap them if second to last is smaller –repeat The idea is that on each pass, the smallest element will “bubble” to the top
11
Bubble Sort 0123456 cbeagfd 0123456 cbeagdf 0123456 cbeadgf 0123456 cbeadgf swap d with f swap d with g no swap swap a with e 0123456 cbaedgf 0123456 cabedgf swap a with b 0123456 acbedgf swap a with c repeat
12
Bubble Sort Code public void bubbleSort(Object[ ] data) { int i, k; for(i = 0; i<data.length – 1; i++) { for(k = data.length – 1; k > i; k- - ) if(((Comparable)data[k]).compareTo(data[k-1]) < 0) swap(k, k-1); }
13
Sorting So Far Advantages –all of the sorting methods discussed so far are fairly simple to implement Disadvantage –all of them operate at O(n 2 ) efficiency –insertion and bubble sort specifics lots of wasted copying may move an item and then move it back
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.