Você está na página 1de 85

SORTING

Merge Sort Algorithm

Given a list L with a length k:


If k == 1 then the list is sorted
Else:
Merge Sort the left side (0 thru k/2)
Merge Sort the right side (k/2+1 thru k)
Merge the right side with the left side

Summary
Divide the unsorted collection into two
Until the sub-arrays only contain one element
Then merge the sub-problem solutions together
Uses divide and conquer strategy

Radix Sort

Radix Sort
Radix Sort is a sorting algorithm that sorts
items by scanning individual digits.
It does not involve comparison between the
items being sorted.
This sort make use of key to shuffle the items.
For integer data items the key is the each
individual digit at each decimal place in that
item for a specific pass.

Radix Sort
We start with Least significant digit(LSD) to
Most significant digit (MSD)
Every integer can be represented by at most k
digits
d1d2dk where di are digits in base r
d1: most significant digit
dk: least significant digit

Radix Sort
Two classifications of radix sort:
For integer sorting: it sorts items from LSD to
MSD
For alphabets sorting: it sorts items from
MSD to LSD.

Radix Sort
Algorithm
Sort by the least significant digit first (counting sort)
=> Numbers with the same digit go to same bin
Reorder all the numbers: the numbers in bin 0 precede the numbers
in bin 1, which precede the numbers in bin 2, and so on

Sort by the next least significant digit


Continue this process until the numbers have been sorted on
all k digits

Radix Sort

Least-significant-digit-first
Example: 275, 087, 426, 061, 509, 170, 677, 503
1st Pass- List becomes
170, 061, 503, 275, 426, 087, 677, 509

Quick Sort

Quick Sort: Uses divide-and-conquer strategy


Below the recursion step is described:
Choose a pivot value. We take the value of the first element
as pivot value, but it can be any value, which is in range of
sorted values, even if it doesn't present in the array.
Partition. Rearrange elements in such a way, that all
elements which are lesser than the pivot go to the left part of
the array and all elements greater than the pivot, go to the
right part of the array.
Sort both parts. Apply quick sort algorithm recursively to
the left and the right parts.

Quicksort Algorithm
Given an array of n elements (e.g., integers):
If array only contains one element, return
Else
pick one element to use as pivot.
Partition elements into two sub-arrays:
Elements less than or equal to pivot
Elements greater than pivot

Quicksort two sub-arrays


Return results

Example
We are given array of n integers to sort:
40

20

10

80

60

50

30 100

Pick Pivot Element


There are a number of ways to pick the pivot element. In this example, we will use
the first element in the array:

40

20

10

80

60

50

30 100

Partitioning Array
Given a pivot, partition the elements of the array such that the
resulting array consists of:
1. One sub-array that contains elements >= pivot
2. Another sub-array that contains elements < pivot
The sub-arrays are stored in the original data array.
Partitioning loops through, swapping elements below/above pivot.

pivot_index = 0

40

20

10

80

60

50

30 100

[0] [1] [2] [3] [4] [5] [6] [7] [8]


i

1. While data[i] <= data[pivot]


++i

pivot_index = 0

40

20

10

80

60

50

30 100

[0] [1] [2] [3] [4] [5] [6] [7] [8]


i

1. While data[i] <= data[pivot]


++i

pivot_index = 0

40

20

10

80

60

50

30 100

[0] [1] [2] [3] [4] [5] [6] [7] [8]


i

1. While data[i] <= data[pivot]


++i

pivot_index = 0

40

20

10

80

60

50

30 100

[0] [1] [2] [3] [4] [5] [6] [7] [8]


i

1. While data[i] <= data[pivot]


++i
2. While data[j] > data[pivot]
--j

pivot_index = 0

40

20

10

80

60

50

30 100

[0] [1] [2] [3] [4] [5] [6] [7] [8]


i

1. While data[i] <= data[pivot]


++i
2. While data[j] > data[pivot]
--j

pivot_index = 0

40

20

10

80

60

50

30 100

[0] [1] [2] [3] [4] [5] [6] [7] [8]


i

1. While data[i] <= data[pivot]


++i
2. While data[j] > data[pivot]
--j
3. If i < j
swap data[i] and data[j]

pivot_index = 0

40

20

10

80

60

50

30 100

[0] [1] [2] [3] [4] [5] [6] [7] [8]


i

1. While data[i] <= data[pivot]


++i
2. While data[j] > data[pivot]
--j
3. If i < j
swap data[i] and data[j]

pivot_index = 0

40

20

10

30

60

50

80 100

[0] [1] [2] [3] [4] [5] [6] [7] [8]


i

1. While data[i] <= data[pivot]


++i
2. While data[j] > data[pivot]
--j
3. If i < j
swap data[i] and data[j]
4. While j > i, go to 1.

pivot_index = 0

40

20

10

30

60

50

80 100

[0] [1] [2] [3] [4] [5] [6] [7] [8]


i

1. While data[i] <= data[pivot]


++i
2. While data[j] > data[pivot]
--j
3. If i< j
swap data[too_big_index] and data[too_small_index]
4. While j > i, go to 1.

pivot_index = 0

40

20

10

30

60

50

80 100

[0] [1] [2] [3] [4] [5] [6] [7] [8]


i

1. While data[i] <= data[pivot]


++i
2. While data[j] > data[pivot]
--j
3. If i < j
swap data[too_big_index] and data[too_small_index]
4. While j > i, go to 1.

pivot_index = 0

40

20

10

30

60

50

80 100

[0] [1] [2] [3] [4] [5] [6] [7] [8]


i

1. While data[i] <= data[pivot]


++i
2. While data[j] > data[pivot]
--j
3. If i < j
swap data[i] and data[j]
4. While j > i, go to 1.

pivot_index = 0

40

20

10

30

60

50

80 100

[0] [1] [2] [3] [4] [5] [6] [7] [8]


i

1. While data[i] <= data[pivot]


++i
2. While data[j] > data[pivot]
--j
3. If i < j
swap data[i] and data[j]
4. While j > i, go to 1.

pivot_index = 0

40

20

10

30

60

50

80 100

[0] [1] [2] [3] [4] [5] [6] [7] [8]


i

1. While data[i] <= data[pivot]


++i
2. While data[j] > data[pivot]
--j
3. If i < j
swap data[i] and data[j]
4. While j> i, go to 1.

pivot_index = 0

40

20

10

30

60

50

80 100

[0] [1] [2] [3] [4] [5] [6] [7] [8]


i

1. While data[i] <= data[pivot]


++i
2. While data[j] > data[pivot]
--j
3. If i < j
swap data[i] and data[j]
4. While j > i, go to 1.

pivot_index = 0

40

20

10

30

50

60

80 100

[0] [1] [2] [3] [4] [5] [6] [7] [8]


i

1. While data[i] <= data[pivot]


++i
2. While data[j] > data[pivot]
--j
3. If i < j
swap data[i] and data[j]
4. While j > i, go to 1.

pivot_index = 0

40

20

10

30

50

60

80 100

[0] [1] [2] [3] [4] [5] [6] [7] [8]


i

1. While data[i] <= data[pivot]


++i
2. While data[j] > data[pivot]
--j
3. If i < j
swap data[i] and data[j]
4. While j > i, go to 1.

pivot_index = 0

40

20

10

30

50

60

80 100

[0] [1] [2] [3] [4] [5] [6] [7] [8]


i

1. While data[i] <= data[pivot]


++i
2. While data[j] > data[pivot]
--j
3. If i < j
swap data[i] and data[j]
4. While j > i, go to 1.

pivot_index = 0

40

20

10

30

50

60

80 100

[0] [1] [2] [3] [4] [5] [6] [7] [8]


i

1. While data[i] <= data[pivot]


++i
2. While data[j] > data[pivot]
--j
3. If i < j
swap data[i] and data[j]
4. While j > i, go to 1.

pivot_index = 0

40

20

10

30

50

60

80 100

[0] [1] [2] [3] [4] [5] [6] [7] [8]


i

1. While data[i] <= data[pivot]


++i
2. While data[j] > data[pivot]
--j
3. If i < j
swap data[i] and data[j]
4. While j > i, go to 1.

pivot_index = 0

40

20

10

30

50

60

80 100

[0] [1] [2] [3] [4] [5] [6] [7] [8]


i

1. While data[i] <= data[pivot]


++i
2. While data[j] > data[pivot]
--j
3. If i < j
swap data[i] and data[j]
4. While j > i, go to 1.

pivot_index = 0

40

20

10

30

50

60

80 100

[0] [1] [2] [3] [4] [5] [6] [7] [8]


i

1. While data[i] <= data[pivot]


++i
2. While data[j] > data[pivot]
--j
3. If i < j
swap data[i] and data[j]
4. While j > i, go to 1.

pivot_index = 0

40

20

10

30

50

60

80 100

[0] [1] [2] [3] [4] [5] [6] [7] [8]


i

1. While data[i] <= data[pivot]


++i
2. While data[j] > data[pivot]
--j
3. If i < j
swap data[i] and data[j]
4. While j > i, go to 1.

pivot_index = 0

40

20

10

30

50

60

80 100

[0] [1] [2] [3] [4] [5] [6] [7] [8]


i

1. While data[ti] <= data[pivot]


++i
2. While data[j] > data[pivot]
--j
3. If i < j
swap data[i] and data[j]
4. While j > i, go to 1.
5. Swap data[j] and data[pivot_index]
pivot_index = 0

40

20

10

30

50

60

80 100

[0] [1] [2] [3] [4] [5] [6] [7] [8]


i

1. While data[i] <= data[pivot]


++i
2. While data[j] > data[pivot]
--j
3. If i < j
swap data[i] and data[j]
4. While j > i, go to 1.
5. Swap data[j] and data[pivot_index]
pivot_index = 4

20

10

30

40

50

60

80 100

[0] [1] [2] [3] [4] [5] [6] [7] [8]


i

Partition Result
7

20

10

30

40

50

60

80 100

[0] [1] [2] [3] [4] [5] [6] [7] [8]


<= data[pivot]

> data[pivot]

Recursion: Quicksort Sub-arrays


7

20

10

30

40

50

60

80 100

[0] [1] [2] [3] [4] [5] [6] [7] [8]


<= data[pivot]

> data[pivot]

Você também pode gostar