Sorting 1 Introduction n Why is it important



![Bubble sorting n For i=1 to n-1 { for j=1 to i if value[i]>value[i+1] Bubble sorting n For i=1 to n-1 { for j=1 to i if value[i]>value[i+1]](https://slidetodoc.com/presentation_image_h2/a8c722f140e318765c183d583412d581/image-4.jpg)
![Insertion sorting n For i=1 to n { temp=value[i]; find where value[i] should be Insertion sorting n For i=1 to n { temp=value[i]; find where value[i] should be](https://slidetodoc.com/presentation_image_h2/a8c722f140e318765c183d583412d581/image-5.jpg)







- Slides: 12

Sorting 1

Introduction n Why is it important n Where to use it 2

Topics of Discussion n Simpler methods (faster coding) Bubble u Insertion u Selection u n Faster methods (faster execution) Merge u Bucket u Radix u 3
![Bubble sorting n For i1 to n1 for j1 to i if valueivaluei1 Bubble sorting n For i=1 to n-1 { for j=1 to i if value[i]>value[i+1]](https://slidetodoc.com/presentation_image_h2/a8c722f140e318765c183d583412d581/image-4.jpg)
Bubble sorting n For i=1 to n-1 { for j=1 to i if value[i]>value[i+1] switch value[i] and value[i+1]; } 4
![Insertion sorting n For i1 to n tempvaluei find where valuei should be Insertion sorting n For i=1 to n { temp=value[i]; find where value[i] should be](https://slidetodoc.com/presentation_image_h2/a8c722f140e318765c183d583412d581/image-5.jpg)
Insertion sorting n 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; } 5

Selection sorting n For i=1 to n { find the biggest value between i and n and switch it with the value in position i; } 6

Merge sorting n n Merge two sorted arrays into a new array e. g. u u n Step 1: new: empty arr 1: 11, 23, 42 arr 2: 9, 25 Step 2: new: 9 arr 1: 11, 23, 42 arr 2: 25 Step 3: new: 9, 11 arr 1: 23, 42 arr 2: 25 etc. 7 An unsorted array of length n can be split

Merge sorting (cont. ) n n 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 8

Bucket sorting n E. g. sort n integers each with a value between 1 and m Create an array arr[] with size m u Pass through the original array and every time the number k occurs, increment arr[k] u Or use a linked list for each value u n Not a very good option when m is very big 9

Radix sorting n n 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. u u n 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 10

Sorting floating point numbers n n n Floating point number x = a*10^b First sort by a and then by b Any base can be used instead of 10 11

Summary n n n 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 12