1 sorting. 2 introduction why is it important where to use it
TRANSCRIPT
![Page 1: 1 Sorting. 2 Introduction Why is it important Where to use it](https://reader036.vdocuments.mx/reader036/viewer/2022082713/5697bf7a1a28abf838c83157/html5/thumbnails/1.jpg)
1
Sorting
![Page 2: 1 Sorting. 2 Introduction Why is it important Where to use it](https://reader036.vdocuments.mx/reader036/viewer/2022082713/5697bf7a1a28abf838c83157/html5/thumbnails/2.jpg)
2
Introduction
Why is it important
Where to use it
![Page 3: 1 Sorting. 2 Introduction Why is it important Where to use it](https://reader036.vdocuments.mx/reader036/viewer/2022082713/5697bf7a1a28abf838c83157/html5/thumbnails/3.jpg)
3
Topics of Discussion
Simpler methods (faster coding) Bubble Insertion Selection
Faster methods (faster execution) Merge Bucket Radix
![Page 4: 1 Sorting. 2 Introduction Why is it important Where to use it](https://reader036.vdocuments.mx/reader036/viewer/2022082713/5697bf7a1a28abf838c83157/html5/thumbnails/4.jpg)
4
Bubble sorting For i=1 to n-1
{
for j=1 to i
if value[i]>value[i+1]
switch value[i] and value[i+1];
}
![Page 5: 1 Sorting. 2 Introduction Why is it important Where to use it](https://reader036.vdocuments.mx/reader036/viewer/2022082713/5697bf7a1a28abf838c83157/html5/thumbnails/5.jpg)
5
Insertion sorting
For i=1 to n{
temp=value[i];find where value[i] should be in
the already sorted values 1 to i-1, e.g. position k;
shift all sorted values after k one place to the right;
value[k]=temp;}
![Page 6: 1 Sorting. 2 Introduction Why is it important Where to use it](https://reader036.vdocuments.mx/reader036/viewer/2022082713/5697bf7a1a28abf838c83157/html5/thumbnails/6.jpg)
6
Selection sorting
For i=1 to n{
find the biggest value between i and n and switch it with the value in position i;
}
![Page 7: 1 Sorting. 2 Introduction Why is it important Where to use it](https://reader036.vdocuments.mx/reader036/viewer/2022082713/5697bf7a1a28abf838c83157/html5/thumbnails/7.jpg)
7
Merge sorting Merge two sorted arrays into a new array e.g.
Step 1: new: emptyarr1: 11, 23, 42arr2: 9, 25
Step 2: new: 9arr1: 11, 23, 42arr2: 25
Step 3: new: 9, 11arr1: 23, 42arr2: 25
etc. An unsorted array of length n can be split into n sorted
arrays of length 1 (an array with only one element is always sorted)
![Page 8: 1 Sorting. 2 Introduction Why is it important Where to use it](https://reader036.vdocuments.mx/reader036/viewer/2022082713/5697bf7a1a28abf838c83157/html5/thumbnails/8.jpg)
8
Merge sorting (cont.)
An unsorted array of length n can be split into n sorted arrays of length 1 (an array with only one element is always sorted)
Recursively merge those n arrays to end with one sorted array
![Page 9: 1 Sorting. 2 Introduction Why is it important Where to use it](https://reader036.vdocuments.mx/reader036/viewer/2022082713/5697bf7a1a28abf838c83157/html5/thumbnails/9.jpg)
9
Bucket sorting
E.g. sort n integers each with a value between 1 and m Create an array arr[] with size m Pass through the original array and
every time the number k occurs, increment arr[k]
Or use a linked list for each value Not a very good option when m is
very big
![Page 10: 1 Sorting. 2 Introduction Why is it important Where to use it](https://reader036.vdocuments.mx/reader036/viewer/2022082713/5697bf7a1a28abf838c83157/html5/thumbnails/10.jpg)
10
Radix sorting
Better for sorting bigger integers Bucker sort using only one digit at a
time, starting with the least significant digit: the last bucket sort alters the final order the most so it should be with the most significant digit. Use a linked list for each value After each bucket sort concatenate the
lists Optimization: use a base larger than 10
or a power of 2
![Page 11: 1 Sorting. 2 Introduction Why is it important Where to use it](https://reader036.vdocuments.mx/reader036/viewer/2022082713/5697bf7a1a28abf838c83157/html5/thumbnails/11.jpg)
11
Sorting floating point numbers Floating point number x = a*10^b First sort by a and then by b Any base can be used instead of
10
![Page 12: 1 Sorting. 2 Introduction Why is it important Where to use it](https://reader036.vdocuments.mx/reader036/viewer/2022082713/5697bf7a1a28abf838c83157/html5/thumbnails/12.jpg)
12
Summary Bubble: loop through and switch places Insertion: find correct place and insert
there Selection: select the next biggest
number and place it Merge: merge sorted arrays or lists
(recursively if necessary) Bucket: create buckets (the values) and
place each item in the right bucket Radix: repeatedly bucket-sort using the
different digits starting from the least significant digit