Vous êtes sur la page 1sur 92

Sorting Algorithms

Sorting Algorithms

Mr. Jignesh Joshi, Data Structures and Algorithms, B.Tech-I (CoED), IIIT Surat, 2017-18
Sorting Algorithms
• Bubble Sort
• Insertion Sort
• Selection Sort
• Merge Sort
• Quicksort
• Radix sort
• Bucket sort
• Heap sort

Mr. Jignesh Joshi, Data Structures and Algorithms, B.Tech-I (CoED), IIIT Surat, 2017-18
Bubble Sort

Mr. Jignesh Joshi, Data Structures and Algorithms, B.Tech-I (CoED), IIIT Surat, 2017-18
Bubble Sort
• Bubble sort works on the same general principal as shaking a soft
drink bottle.

Mr. Jignesh Joshi, Data Structures and Algorithms, B.Tech-I (CoED), IIIT Surat, 2017-18
Bubble Sort
• Bubble sort works on the same general principal as shaking a soft
drink bottle.
• Right after shaking, the contents are a mixture of bubbles and soft
drink, distributed randomly.

Mr. Jignesh Joshi, Data Structures and Algorithms, B.Tech-I (CoED), IIIT Surat, 2017-18
Bubble Sort
• Bubble sort works on the same general principal as shaking a soft
drink bottle.
• Right after shaking, the contents are a mixture of bubbles and soft
drink, distributed randomly.
• Because bubbles are lighter than the soft drink, they rise to the
surface, displacing the soft drink downwards.

Mr. Jignesh Joshi, Data Structures and Algorithms, B.Tech-I (CoED), IIIT Surat, 2017-18
Bubble Sort
• Bubble sort works on the same general principal as shaking a soft
drink bottle.
• Right after shaking, the contents are a mixture of bubbles and soft
drink, distributed randomly.
• Because bubbles are lighter than the soft drink, they rise to the
surface, displacing the soft drink downwards.
• This is how bubble sort got its name, because the smaller elements
“float” to the top, while the larger elements “sink” to bottom.

Mr. Jignesh Joshi, Data Structures and Algorithms, B.Tech-I (CoED), IIIT Surat, 2017-18
Bubble Sort
• Bubble sort works on the same general principal as shaking a soft
drink bottle.
• Right after shaking, the contents are a mixture of bubbles and soft
drink, distributed randomly.
• Because bubbles are lighter than the soft drink, they rise to the
surface, displacing the soft drink downwards.
• This is how bubble sort got its name, because the smaller elements
“float” to the top, while the larger elements “sink” to bottom.
• Bubble sort, sometimes referred to as sinking sort.

Mr. Jignesh Joshi, Data Structures and Algorithms, B.Tech-I (CoED), IIIT Surat, 2017-18
Bubble Sort …
• Bubble sort examines the array from start to finish, comparing
elements as it goes.

Mr. Jignesh Joshi, Data Structures and Algorithms, B.Tech-I (CoED), IIIT Surat, 2017-18
Bubble Sort …
• Bubble sort examines the array from start to finish, comparing
elements as it goes.
• Any time it finds a larger element before a smaller element, it swaps
the two.

Mr. Jignesh Joshi, Data Structures and Algorithms, B.Tech-I (CoED), IIIT Surat, 2017-18
Bubble Sort …
• Bubble sort examines the array from start to finish, comparing
elements as it goes.
• Any time it finds a larger element before a smaller element, it swaps
the two.
• In this way, the larger elements are passed towards the end.

Mr. Jignesh Joshi, Data Structures and Algorithms, B.Tech-I (CoED), IIIT Surat, 2017-18
Bubble Sort …
• Bubble sort examines the array from start to finish, comparing
elements as it goes.
• Any time it finds a larger element before a smaller element, it swaps
the two.
• In this way, the larger elements are passed towards the end.
• The largest element of the array therefore "bubbles" to the end of the
array.

Mr. Jignesh Joshi, Data Structures and Algorithms, B.Tech-I (CoED), IIIT Surat, 2017-18
Bubble Sort …
• Bubble sort examines the array from start to finish, comparing
elements as it goes.
• Any time it finds a larger element before a smaller element, it swaps
the two.
• In this way, the larger elements are passed towards the end.
• The largest element of the array therefore "bubbles" to the end of the
array.
• Then it repeats the process for the unsorted portion of the array until
the whole array is sorted.

Mr. Jignesh Joshi, Data Structures and Algorithms, B.Tech-I (CoED), IIIT Surat, 2017-18
Bubble Sort: Example

Mr. Jignesh Joshi, Data Structures and Algorithms, B.Tech-I (CoED), IIIT Surat, 2017-18
Bubble Sort: Algorithm

Mr. Jignesh Joshi, Data Structures and Algorithms, B.Tech-I (CoED), IIIT Surat, 2017-18
Bubble Sort: Algorithm
Algorithm BUBBLE_SORT (K,N)
Input: Given a vector K of N elements.
Output: Sorted vector in ascending (increasing) order.
1. [Initialize]
𝐿𝐴𝑆𝑇 𝑁
2.[Loop on pass index]
Repeat thru step 4 for 𝑃𝐴𝑆𝑆 = 1, 2, … , 𝑁 − 1
3.[Perform pairwise comparisons on unsorted elements]
Repeat for 𝐼 = 1, 2, … , 𝐿𝐴𝑆𝑇 − 1
If 𝐾[𝐼] > 𝐾[𝐼 + 1]
then 𝐾[𝐼] ↔ 𝐾[𝐼 + 1]
4. [Reduce size of unsorted list]
𝐿𝐴𝑆𝑇  𝐿𝐴𝑆𝑇 − 1
5.[Finished]
Return

Mr. Jignesh Joshi, Data Structures and Algorithms, B.Tech-I (CoED), IIIT Surat, 2017-18
Modified Bubble Sort: Example

Mr. Jignesh Joshi, Data Structures and Algorithms, B.Tech-I (CoED), IIIT Surat, 2017-18
Modified Bubble Sort: Algorithm
Algorithm MBUBBLE_SORT (K,N) Repeat for 𝐼 = 1, 2, … , 𝐿𝐴𝑆𝑇 − 1
Input: Given a vector K of N elements. If 𝐾[𝐼] > 𝐾[𝐼 + 1]
Output: Sorted vector in ascending (increasing) order. then 𝐾[𝐼] ↔ 𝐾[𝐼 + 1]
1. [Initialize] 𝐸𝑋𝐶𝐻𝑆  𝐸𝑋𝐶𝐻𝑆 + 1
𝐿𝐴𝑆𝑇 𝑁 5. [Were any exchanges made on this pass ?]
2.[Loop on pass index] If 𝐸𝑋𝐶𝐻𝑆 = 0
Repeat thru step 5 for 𝑃𝐴𝑆𝑆 = 1, 2, … , 𝑁 − 1 then Return (mission accomplished;
return early)
3. [Initialize exchange counter for this pass]
else 𝐿𝐴𝑆𝑇  𝐿𝐴𝑆𝑇 − 1
𝐸𝑋𝐶𝐻𝑆  0
6.[Finished]
4.[Perform pairwise comparisons on unsorted elements]
Return

Mr. Jignesh Joshi, Data Structures and Algorithms, B.Tech-I (CoED), IIIT Surat, 2017-18
Insertion Sort

Mr. Jignesh Joshi, Data Structures and Algorithms, B.Tech-I (CoED), IIIT Surat, 2017-18
Insertion Sort

Mr. Jignesh Joshi, Data Structures and Algorithms, B.Tech-I (CoED), IIIT Surat, 2017-18
Insertion Sort: Algorithm

Mr. Jignesh Joshi, Data Structures and Algorithms, B.Tech-I (CoED), IIIT Surat, 2017-18
Insertion Sort: Algorithm
Algorithm INSERTION_SORT (A,N)
Input: Given a vector A of N elements.
Output: Sorted vector in ascending (increasing) order.
1.[Loop]
Repeat thru step 3 for 𝑗 = 2, … , 𝑁
𝑘𝑒𝑦  𝐴[𝑗]
𝑖𝑗 − 1
2. [Insert A[j] into the sorted sequence A[1 … j - 1] ]
Repeat for 𝑖 > 0 𝑎𝑛𝑑 𝐴 𝑖 > 𝑘𝑒𝑦
𝐴[𝑖 + 𝑖]  𝐴[𝑖]
𝑖𝑖−1
3. [Insert the key value at right position]
𝐴[𝑖 + 1]  𝑘𝑒𝑦

Mr. Jignesh Joshi, Data Structures and Algorithms, B.Tech-I (CoED), IIIT Surat, 2017-18
Selection Sort

Mr. Jignesh Joshi, Data Structures and Algorithms, B.Tech-I (CoED), IIIT Surat, 2017-18
Selection Sort
• Consider sorting n numbers stored in array A by first finding the
smallest element of A and exchanging it with the element in A[1].
• Then find the second smallest element of A, and exchange it with
A[2].
• Continue in this manner for the first n-1 elements of A.
• Write this algorithm, which is known as selection sort.

Mr. Jignesh Joshi, Data Structures and Algorithms, B.Tech-I (CoED), IIIT Surat, 2017-18
Selection Sort: Example

Mr. Jignesh Joshi, Data Structures and Algorithms, B.Tech-I (CoED), IIIT Surat, 2017-18
Selection Sort: Algorithm
Algorithm SELECTION_SORT (K,N) 4. [Exchange elements]
Input: Given a vector K of N elements. If 𝑀𝐼𝑁_𝐼𝑁𝐷𝐸𝑋 ≠ 𝑃𝐴𝑆𝑆
Output: Sorted vector in ascending (increasing) order. then K[PASS] ↔ 𝐾{𝑀𝐼𝑁_𝐼𝑁𝐷𝐸𝑋}
1.[Loop on pass index] 5.[Finished]
Repeat thru step 4 for 𝑃𝐴𝑆𝑆 = 1, 2, … , 𝑁 − 1 Return
2.[Initialize minimum index]
𝑀𝐼𝑁_𝐼𝑁𝐷𝐸𝑋 𝑃𝐴𝑆𝑆
3. [Make a pass and obtain element with smallest value]
Repeat for 𝐼 = 𝑃𝐴𝑆𝑆 + 1, 𝑃𝐴𝑆𝑆 + 2, … , 𝑁
If 𝐾 𝐼 < 𝐾[𝑀𝐼𝑁_𝐼𝑁𝐷𝐸𝑋]
then 𝑀𝐼𝑁_𝐼𝑁𝐷𝐸𝑋𝐼

Mr. Jignesh Joshi, Data Structures and Algorithms, B.Tech-I (CoED), IIIT Surat, 2017-18
Merge Sort

Mr. Jignesh Joshi, Data Structures and Algorithms, B.Tech-I (CoED), IIIT Surat, 2017-18
Merge Sort: Example

Mr. Jignesh Joshi, Data Structures and Algorithms, B.Tech-I (CoED), IIIT Surat, 2017-18
Merge Sort: Algorithm
• Merge sort algorithm contains two main procedure.
• SIMPLE_MERGE
• TWO_WAY_MERGE_SORT

Mr. Jignesh Joshi, Data Structures and Algorithms, B.Tech-I (CoED), IIIT Surat, 2017-18
Merge Sort: Algorithm
Algorithm SIMPLE_MERGE (K, FIRST, SECOND, THIRD) If 𝐼 ≥ 𝑆𝐸𝐶𝑂𝑁𝐷
1.[Initialize] then Repeat while 𝐽 ≤ 𝑇𝐻𝐼𝑅𝐷
𝐼  𝐹𝐼𝑅𝑆𝑇 𝐿𝐿+1
𝐽  𝑆𝐸𝐶𝑂𝑁𝐷 TEMP[𝐿]  𝐾[𝐽]
𝐿0 𝐽𝐽+1
2. [Compare corresponding elements and output the smallest] else Repeat while 𝐼 < 𝑆𝐸𝐶𝑂𝑁𝐷
Repeat while 𝐼 < 𝑆𝐸𝐶𝑂𝑁𝐷 𝑎𝑛𝑑 𝐽 ≤ 𝑇𝐻𝐼𝑅𝐷 𝐿𝐿+1
If 𝐾 𝐼 ≤ 𝐾[𝐽] TEMP[𝐿]  𝐾[𝐼]
then 𝐿  𝐿 + 1 𝐼𝐼+1
TEMP[𝐿]  𝐾[𝐼] 4.[Copy elements in temporary vector into original area]
𝐼𝐼+1 Repeat for 𝐼 = 1, 2, … , 𝐿
else 𝐿  𝐿 + 1 K FIRST − 1 + I  TEMP 𝐼
TEMP[𝐿]  𝐾[𝐽] 5. [Finished]
𝐽𝐽+1 Return
3. [Copy the remaining unprocessed elements in output area]

Mr. Jignesh Joshi, Data Structures and Algorithms, B.Tech-I (CoED), IIIT Surat, 2017-18
Merge Sort: Algorithm
Algorithm TWO_WAY_MERGE_SORT(K, START, 5.[Recursively sort second subtable]
FINISH)
Call TWO_WAY_MERGE_SORT(K,
1.[Compute the size of the current subtable/vector]
MIDDLE+1, FINISH)
𝑆𝐼𝑍𝐸  𝐹𝐼𝑁𝐼𝑆𝐻 − 𝑆𝑇𝐴𝑅𝑇 + 1
6. [Merge two ordered subtables]
2. [Test base condition for subtable of size one]
Call SIMPLE_MERGE(K, START,
If 𝑆𝐼𝑍𝐸 ≤ 1
MIDDLE+1, FINISH)
then Return
7. [Finished]
3. [Calculate the midpoint position of current subtable]
Return
𝑀𝐼𝐷𝐷𝐿𝐸  𝑆𝑇𝐴𝑅𝑇 + 𝑆𝐼𝑍𝐸/2 − 1
4.[Recursively sort first subtable]
Call TWO_WAY_MERGE_SORT(K, START,
MIDDLE)

Mr. Jignesh Joshi, Data Structures and Algorithms, B.Tech-I (CoED), IIIT Surat, 2017-18
Merge Sort
• Merge sort (also commonly spelled mergesort) is an efficient, general-
purpose, comparison-based sorting algorithm.

Mr. Jignesh Joshi, Data Structures and Algorithms, B.Tech-I (CoED), IIIT Surat, 2017-18
Merge Sort
• Merge sort (also commonly spelled mergesort) is an efficient, general-
purpose, comparison-based sorting algorithm.
• Most implementations produce a stable sort, which means that the
implementation preserves the input order of equal elements in the
sorted output.

Mr. Jignesh Joshi, Data Structures and Algorithms, B.Tech-I (CoED), IIIT Surat, 2017-18
Quick Sort

Mr. Jignesh Joshi, Data Structures and Algorithms, B.Tech-I (CoED), IIIT Surat, 2017-18
Quick Sort: Example1

Mr. Jignesh Joshi, Data Structures and Algorithms, B.Tech-I (CoED), IIIT Surat, 2017-18
Quick Sort: Example1

Mr. Jignesh Joshi, Data Structures and Algorithms, B.Tech-I (CoED), IIIT Surat, 2017-18
Quick Sort: Example2

Mr. Jignesh Joshi, Data Structures and Algorithms, B.Tech-I (CoED), IIIT Surat, 2017-18
Quick Sort: Example2

Mr. Jignesh Joshi, Data Structures and Algorithms, B.Tech-I (CoED), IIIT Surat, 2017-18
Quick Sort: Pseudo-code

Mr. Jignesh Joshi, Data Structures and Algorithms, B.Tech-I (CoED), IIIT Surat, 2017-18
Quick Sort: Pseudo-code

Mr. Jignesh Joshi, Data Structures and Algorithms, B.Tech-I (CoED), IIIT Surat, 2017-18
Quick Sort
• Quicksort (sometimes called partition-exchange sort) is an efficient
sorting algorithm.

Mr. Jignesh Joshi, Data Structures and Algorithms, B.Tech-I (CoED), IIIT Surat, 2017-18
Quick Sort
• Quicksort (sometimes called partition-exchange sort) is an efficient
sorting algorithm.
• Quicksort is a comparison sort, meaning that it can sort items of any
type for which a "less-than" relation is defined.

Mr. Jignesh Joshi, Data Structures and Algorithms, B.Tech-I (CoED), IIIT Surat, 2017-18
Quick Sort
• Quicksort (sometimes called partition-exchange sort) is an efficient
sorting algorithm.
• Quicksort is a comparison sort, meaning that it can sort items of any
type for which a "less-than" relation is defined.
• In efficient implementations it is not a stable sort, meaning that the
relative order of equal sort items is not preserved.

Mr. Jignesh Joshi, Data Structures and Algorithms, B.Tech-I (CoED), IIIT Surat, 2017-18
Radix sort

Mr. Jignesh Joshi, Data Structures and Algorithms, B.Tech-I (CoED), IIIT Surat, 2017-18
Radix sort
• Radix sort is a non-comparative integer sorting algorithm that sorts
data with integer keys by grouping keys by the individual digits which
share the same significant position and value.

Mr. Jignesh Joshi, Data Structures and Algorithms, B.Tech-I (CoED), IIIT Surat, 2017-18
Radix sort
• Radix sort is a non-comparative integer sorting algorithm that sorts
data with integer keys by grouping keys by the individual digits which
share the same significant position and value.
• There are two classification of Radix sort

Mr. Jignesh Joshi, Data Structures and Algorithms, B.Tech-I (CoED), IIIT Surat, 2017-18
Radix sort
• Radix sort is a non-comparative integer sorting algorithm that sorts
data with integer keys by grouping keys by the individual digits which
share the same significant position and value.
• There are two classification of Radix sort
• Least significant digit (LSD)

Mr. Jignesh Joshi, Data Structures and Algorithms, B.Tech-I (CoED), IIIT Surat, 2017-18
Radix sort
• Radix sort is a non-comparative integer sorting algorithm that sorts
data with integer keys by grouping keys by the individual digits which
share the same significant position and value.
• There are two classification of Radix sort
• Least significant digit (LSD)
• Most significant digit (MSD)

Mr. Jignesh Joshi, Data Structures and Algorithms, B.Tech-I (CoED), IIIT Surat, 2017-18
Radix sort (LSD)

Mr. Jignesh Joshi, Data Structures and Algorithms, B.Tech-I (CoED), IIIT Surat, 2017-18
Radix sort (LSD)
• Least Significant Digit (LSD)

• Definition: Each key is first figuratively dropped into one level of


buckets corresponding to the value of the rightmost digit. Each bucket
preserves the original order of the keys as the keys are dropped into
the bucket. There is a one-to-one correspondence between the
buckets and the values that can be represented by the rightmost
digit. Then, the process repeats with the next neighboring more
significant digit until there are no more digits to process.

Mr. Jignesh Joshi, Data Structures and Algorithms, B.Tech-I (CoED), IIIT Surat, 2017-18
Radix sort (LSD) …
• In other words
• Take the least significant digit of each key
• Group the keys based on that digit, but otherwise keep the original
order of keys.
• Repeat the grouping process with each more significant digit.

Mr. Jignesh Joshi, Data Structures and Algorithms, B.Tech-I (CoED), IIIT Surat, 2017-18
Radix sort (LSD) …
Example
Original, unsorted list:
170, 45, 75, 90, 802, 2, 24, 66

Mr. Jignesh Joshi, Data Structures and Algorithms, B.Tech-I (CoED), IIIT Surat, 2017-18
Radix sort (LSD) …
Example
Original, unsorted list:
170, 45, 75, 90, 802, 2, 24, 66
Sorting by least significant digit (1s place) gives:
170, 90, 802, 2, 24, 45, 75, 66

Mr. Jignesh Joshi, Data Structures and Algorithms, B.Tech-I (CoED), IIIT Surat, 2017-18
Radix sort (LSD) …
Example
Original, unsorted list:
170, 45, 75, 90, 802, 2, 24, 66
Sorting by least significant digit (1s place) gives:
170, 90, 802, 2, 24, 45, 75, 66
Sorting by next digit (10s place) gives:
802, 2, 24, 45, 66, 170, 75, 90

Mr. Jignesh Joshi, Data Structures and Algorithms, B.Tech-I (CoED), IIIT Surat, 2017-18
Radix sort (LSD) …
Example
Original, unsorted list:
170, 45, 75, 90, 802, 2, 24, 66
Sorting by least significant digit (1s place) gives:
170, 90, 802, 2, 24, 45, 75, 66
Sorting by next digit (10s place) gives:
802, 2, 24, 45, 66, 170, 75, 90
Sorting by most significant digit (100s place) gives:
2, 24, 45, 66, 75, 90, 170, 802
Mr. Jignesh Joshi, Data Structures and Algorithms, B.Tech-I (CoED), IIIT Surat, 2017-18
Radix sort (LSD) …
Iterative version using queues

Mr. Jignesh Joshi, Data Structures and Algorithms, B.Tech-I (CoED), IIIT Surat, 2017-18
Radix sort (LSD) …
Iterative version using queues
1. The integers are enqueued into an array of ten separate queues
based on their digits from right to left. Computers often represent
integers internally as fixed-length binary digits. Here, we will do
something analogous with fixed-length decimal digits. So, using the
numbers from the previous example, the queues for the 1st pass
would be:

Mr. Jignesh Joshi, Data Structures and Algorithms, B.Tech-I (CoED), IIIT Surat, 2017-18
Radix sort (LSD) …
Iterative version using queues…

0: 170, 090
1: none
2: 802, 002
3: none
4: 024
5: 045, 075
6: 066
7–9: none

Mr. Jignesh Joshi, Data Structures and Algorithms, B.Tech-I (CoED), IIIT Surat, 2017-18
Radix sort (LSD) …
Iterative version using queues…
2. The queues are dequeued back into an array of integers, in
increasing order. Using the same numbers, the array will look like
this after the first pass:

Mr. Jignesh Joshi, Data Structures and Algorithms, B.Tech-I (CoED), IIIT Surat, 2017-18
Radix sort (LSD) …
Iterative version using queues…
2. The queues are dequeued back into an array of integers, in
increasing order. Using the same numbers, the array will look like
this after the first pass:
170, 090, 802, 002, 024, 045, 075, 066

Mr. Jignesh Joshi, Data Structures and Algorithms, B.Tech-I (CoED), IIIT Surat, 2017-18
Radix sort (LSD) …
Iterative version using queues…
3. For the second pass:

Mr. Jignesh Joshi, Data Structures and Algorithms, B.Tech-I (CoED), IIIT Surat, 2017-18
Radix sort (LSD) …
Iterative version using queues…
3. For the second pass:
Queues:
0: 802, 002
1: none
2: 024
3: none
4: 045
5: none
6: 066
7: 170, 075
8: none
9: 090

Mr. Jignesh Joshi, Data Structures and Algorithms, B.Tech-I (CoED), IIIT Surat, 2017-18
Radix sort (LSD) …
Iterative version using queues…
3. For the second pass:
Queues:
0: 802, 002 Array:
1: none
2: 024 802, 002, 024, 045, 066, 170, 075, 090
3: none
4: 045 (note that at this point only 802 and 170
are out of order)
5: none
6: 066
7: 170, 075
8: none
9: 090

Mr. Jignesh Joshi, Data Structures and Algorithms, B.Tech-I (CoED), IIIT Surat, 2017-18
Radix sort (LSD) …
Iterative version using queues…
4. For the third pass:
Queues:
0: 002, 024, 045, 066, 075, 090
1: 170
2–7: none
8: 802
9: none

Mr. Jignesh Joshi, Data Structures and Algorithms, B.Tech-I (CoED), IIIT Surat, 2017-18
Radix sort (LSD) …
Iterative version using queues…
4. For the third pass:
Queues:
0: 002, 024, 045, 066, 075, 090
1: 170
2–7: none
8: 802
9: none
Array:
002, 024, 045, 066, 075, 090, 170, 802 (sorted)

Mr. Jignesh Joshi, Data Structures and Algorithms, B.Tech-I (CoED), IIIT Surat, 2017-18
Radix sort: Algorithm

Mr. Jignesh Joshi, Data Structures and Algorithms, B.Tech-I (CoED), IIIT Surat, 2017-18
Radix sort: Algorithm
General algorithm for Radix sort
1. Repeat thru step 6 for each digit in the key
2. Initialize the pockets
3. Repeat thru step 5 until the end of the linked list
4. Obtain the next digit of the key
5. Insert the record in the appropriate pocket
6. Combine the pockets to form a new linked list

Mr. Jignesh Joshi, Data Structures and Algorithms, B.Tech-I (CoED), IIIT Surat, 2017-18
Radix sort: Example
Input : 42, 23, 74, 11, 65, 57, 94, 36, 99, 87, 70, 81, 61

Mr. Jignesh Joshi, Data Structures and Algorithms, B.Tech-I (CoED), IIIT Surat, 2017-18
Radix sort: Example
Input : 42, 23, 74, 11, 65, 57, 94, 36, 99, 87, 70, 81, 61

Mr. Jignesh Joshi, Data Structures and Algorithms, B.Tech-I (CoED), IIIT Surat, 2017-18
Bucket sort

Mr. Jignesh Joshi, Data Structures and Algorithms, B.Tech-I (CoED), IIIT Surat, 2017-18
Bucket sort
• Bucket sort, or bin sort, is a sorting algorithm that works by
distributing the elements of an array into a number of buckets.

Mr. Jignesh Joshi, Data Structures and Algorithms, B.Tech-I (CoED), IIIT Surat, 2017-18
Bucket sort
• Bucket sort, or bin sort, is a sorting algorithm that works by
distributing the elements of an array into a number of buckets.
• Each bucket is then sorted individually.

Mr. Jignesh Joshi, Data Structures and Algorithms, B.Tech-I (CoED), IIIT Surat, 2017-18
Bucket sort
• Bucket sort, or bin sort, is a sorting algorithm that works by
distributing the elements of an array into a number of buckets.
• Each bucket is then sorted individually.
• using a different sorting algorithm.
• by recursively applying the bucket sorting algorithm.

Mr. Jignesh Joshi, Data Structures and Algorithms, B.Tech-I (CoED), IIIT Surat, 2017-18
Bucket sort
• Bucket sort, or bin sort, is a sorting algorithm that works by
distributing the elements of an array into a number of buckets.
• Each bucket is then sorted individually.
• using a different sorting algorithm.
• by recursively applying the bucket sorting algorithm.
• It is a distribution sort and is a cousin of radix sort in the most-to-least
significant digit flavor.

Mr. Jignesh Joshi, Data Structures and Algorithms, B.Tech-I (CoED), IIIT Surat, 2017-18
Bucket sort
• Bucket sort, or bin sort, is a sorting algorithm that works by
distributing the elements of an array into a number of buckets.
• Each bucket is then sorted individually.
• using a different sorting algorithm.
• by recursively applying the bucket sorting algorithm.
• It is a distribution sort and is a cousin of radix sort in the most-to-least
significant digit flavor.
• Bucket sort can be implemented with comparisons and therefore can
also be considered a comparison sort algorithm.

Mr. Jignesh Joshi, Data Structures and Algorithms, B.Tech-I (CoED), IIIT Surat, 2017-18
Bucket sort: Example

Mr. Jignesh Joshi, Data Structures and Algorithms, B.Tech-I (CoED), IIIT Surat, 2017-18
Bucket sort: Example

Mr. Jignesh Joshi, Data Structures and Algorithms, B.Tech-I (CoED), IIIT Surat, 2017-18
Bucket sort: Example

Mr. Jignesh Joshi, Data Structures and Algorithms, B.Tech-I (CoED), IIIT Surat, 2017-18
Bucket sort: Example

Elements are distributed among bins

Mr. Jignesh Joshi, Data Structures and Algorithms, B.Tech-I (CoED), IIIT Surat, 2017-18
Bucket sort: Example

Elements are distributed among bins

Then,
Mr. Jignesh Joshi, Data Structures and Algorithms, B.Tech-Ielements are2017-18
(CoED), IIIT Surat, sorted within each bin
Bucket sort: Pseudo-code

Mr. Jignesh Joshi, Data Structures and Algorithms, B.Tech-I (CoED), IIIT Surat, 2017-18
Bucket sort: Pseudo-code
function bucketSort(array, n)
1. buckets ← new array of n empty lists
2. for i = 0 to (length(array)-1) do
insert array[i] into buckets[msbits(array[i], k)]
3. for i = 0 to n - 1 do
nextSort(buckets[i]);
4. return the concatenation of buckets[0], ...., buckets[n-1]

Mr. Jignesh Joshi, Data Structures and Algorithms, B.Tech-I (CoED), IIIT Surat, 2017-18
Heap sort

Mr. Jignesh Joshi, Data Structures and Algorithms, B.Tech-I (CoED), IIIT Surat, 2017-18
Data Structure: Heap

Mr. Jignesh Joshi, Data Structures and Algorithms, B.Tech-I (CoED), IIIT Surat, 2017-18
Data Structure: Heap
• Binary heap
• A binary heap is a heap data structure that takes the form of a binary
tree.

Mr. Jignesh Joshi, Data Structures and Algorithms, B.Tech-I (CoED), IIIT Surat, 2017-18
Data Structure: Heap
• Binary heap
• A binary heap is a heap data structure that takes the form of a binary
tree.
• A binary heap is defined as a binary tree with two additional
constraints:

Mr. Jignesh Joshi, Data Structures and Algorithms, B.Tech-I (CoED), IIIT Surat, 2017-18
Data Structure: Heap
• Binary heap
• A binary heap is a heap data structure that takes the form of a binary
tree.
• A binary heap is defined as a binary tree with two additional
constraints:
• Shape property: a binary heap is a complete binary tree; that is, all levels of
the tree, except possibly the last one (deepest) are fully filled, and, if the last
level of the tree is not complete, the nodes of that level are filled from left to
right.

Mr. Jignesh Joshi, Data Structures and Algorithms, B.Tech-I (CoED), IIIT Surat, 2017-18
Data Structure: Heap
• Binary heap
• A binary heap is a heap data structure that takes the form of a binary
tree.
• A binary heap is defined as a binary tree with two additional
constraints:
• Shape property: a binary heap is a complete binary tree; that is, all levels of
the tree, except possibly the last one (deepest) are fully filled, and, if the last
level of the tree is not complete, the nodes of that level are filled from left to
right.
• Heap property: the key stored in each node is either greater than or equal to
(≥) or less than or equal to (≤) the keys in the node's children, according to
some total order.

Mr. Jignesh Joshi, Data Structures and Algorithms, B.Tech-I (CoED), IIIT Surat, 2017-18
Data Structure: Heap
• Types of binary heap

Mr. Jignesh Joshi, Data Structures and Algorithms, B.Tech-I (CoED), IIIT Surat, 2017-18
Data Structure: Heap
• Types of binary heap
• max-heaps: parent key is greater than or equal to (≥) the child keys

Mr. Jignesh Joshi, Data Structures and Algorithms, B.Tech-I (CoED), IIIT Surat, 2017-18
Data Structure: Heap
• Types of binary heap
• max-heaps: parent key is greater than or equal to (≥) the child keys
• min-heaps: parent key is less than or equal to (≤) the child keys

Mr. Jignesh Joshi, Data Structures and Algorithms, B.Tech-I (CoED), IIIT Surat, 2017-18

Vous aimerez peut-être aussi