Sunteți pe pagina 1din 63

Sorting

Sorting is the process of arranging a set of


elements in a specific order.
Sorting can be classified in to
Internal sorting
External sorting

Classification
Internal
All of the data are
held in the primary
memory during the
sorting process.

External
Uses primary
memory for the data
currently being
sorted and
secondary storage
for any data that
does not fit in
primary memory.

Bubble Sort
Traverse a collection of elements
Move from the front to the end
Bubble the largest value to the end using pairwise comparisons and swapping
1
77

2
42

35

12

101

7742 Swap4277

35

12

42

7735

3577

42

2
35

1
42

12

101
5

77
7712 Swap 12

101

12

4
77

101

2
35

6
5
6
5

5
101

No need to swap

6
5

1
42

35

1
42

12

2
35

3
12

77

1015

Swap 101
5

77

Largest value correctly placed

101

Notice that only the largest value is correctly


placed
All other values are still out of order
So we need to repeat this process
1
42

2
35

3
12

4
77

Largest value correctly placed

5
5

6
101

The Algorithm
index <- 1
last_compare_at <-(n1)
loop
exit if(index > last_compare_at)
if(A[index] > A[index + 1]) then
Swap(A[index], A[index + 1])
end if
index <- index+1
End loop
* Complexity of bubble sort:
In best case=worst case=average case=0(n2)

Bubbling All the Elements


1
42

2
35

N-1

35

2
12

12

35

35
2

12

3
35

5
5

6
101

4
5

3
42

4
77

12

12

77

101

42

77

101

4
42
4
42

5
77
5
77

6
101
6
101

Insertion sort eg:

The Algorithm
for i=2 to n do
begin
j := i
while (a[j] < a[j-1])
begin
swap ( a[j],a[j-1] )
j = j +1
end
end

Complexity of insertion sort:


In best case= 0(n);worst case=average case=0(n2)

Selection Sort
The smallest element is selected from the
unsorted sublist and exchange with the
element at the beginning of the unsorted list.

Algorithm
set current to 0
loop (until last element sorted)
set smallest to current
set index to current + 1
loop (index < last)
if (index key < smallest key)
set smallest to index
increment index
end loop
smallest selected; exchange with current element.
exchange (current, smallest)
increment current
end loop
End selection Sort

Selection Sort
23

78

45

32

56

Original list

32

56

After pass 1

32

56

After pass 2

56

After pass 3

Unsorted

78

Sorted

45

Unsorted

23

45

Sorted

23

23

78

Unsorted

32

78

Sorted

23

45

Unsorted

32

45

23

56

After pass 4

Unsorted

Sorted

78

32

45
Sorted

56

78

After pass 5

Quicksort
To sort a[left...right]:
if left < right:

Partition a[left...right] such that:


all a[left...p-1] are less than a[p], and
all a[p+1...right] are >= a[p]
Quicksort a[left...p-1]
Quicksort a[p+1...right]
Terminate

A key step in the Quicksort algorithm is


partitioning the array
We choose some (any) number p in the array
to use as a pivot
We partition the array into three parts:
p

numbers
less than p

numbers greater
than or equal to p

Choose an array value (say, the first) to use as


the pivot
Starting from the left end, find the first
element that is greater than or equal to the
pivot
Searching backward from the right end, find
the first element that is less than the pivot
Interchange (swap) these two elements
Repeat, searching from where we left off, until
done
Quicksort is usually O(n log2n)

To partition a[left...right]:

Set p = a[left], l = left + 1, r = right;


while l < r, do
while l < right & a[l] < p, set l = l + 1
while r > left & a[r] >= p, set r = r - 1
if l < r, swap a[l] and a[r]

Set a[left] = a[r], a[r] = p

Terminate

Mergesort
A divide-and-conquer algorithm:
Divide the unsorted array into 2 halves until
the sub-arrays only contain one element
Merge the sub-problem solutions together:
Compare the sub-arrays first elements
Remove the smallest element and put it into the
result array
Continue the process until all elements have been
put into the result array
37

23

89

15

12

19

98

23

45

14

67

33

42

98
98

23

23
45

45
14

14

67
6

33
67

42
33

42

98
98

98

23

23

23

45

45

45

14

14

14

67
6

33
67

42
33

42

98
98

98
98

23

23
23

23

45

45

45

14

14

14

67
6

33
67

42
33

42

98
98

98
98

Merge

23

23
23

23

45

45

45

14

14

14

67
6

33
67

42
33

42

98
98

98
98

23
Merge

23

23
23

23

45

45

45

14

14

14

67
6

33
67

42
33

42

98
98

98
98

23
Merge

23

23
23

98

23

45

45

45

14

14

14

67
6

33
67

42
33

42

98
98

98
98

23

23

23
23

98

23

45

45

45
45

14

14

67
6

14
14

33
67

42
33

42

98
98

98
98

23

23

23
23

23

45

45

45

14

14

98
Merge

67
6

14

45

14

33
67

42
33

42

98
98

98
98

23

23

23
23

98

23

45

45

45

14

14

14
Merge

67
6

14

45

14

33
67

42
33

42

98
98

98
98

23

23

23
23

98

23

45

45

45

14

14

14

14

45

Merge

67
6

14

45

33
67

42
33

42

98
98

98
98

23

23

23

45

45

23
23

45
45

98

14

Merge

14

14

67
6

14
14

45

33
67

42
33

42

98
98

98
98

23

23

23

45

45

23
23

45
45

98

14

14
Merge

14

14

67
6

14
14

45

33
67

42
33

42

98
98

98
98

23
14

23

23

45

45

23

45

23

45

98

14

23
Merge

14

14

67
6

14
14

45

33
67

42
33

42

98
98

98
98

23
14

23

23

45

45

23

45

23

45

98

14

23

45

Merge

14

14

67
6

14
14

45

33
67

42
33

42

98
98

98
98

23
14

23

23

45

45

23

14

14

45

23

98

14

23

45

Merge

14

45
98

67
6

14

45

33
67

42
33

42

98
98

98
98

23
14

23

23

45

45

23
23

14

45

14

45

98
23

14

14
45

14

45
98

67

33

67

67

42
33

42

33

42

98
98

98
98

23
14

23

23

45

45

23
23

14

45

14

45

98
23

14

14
45

14

45
98

67

33

67

67

67

42
33

42

33

42

98
98

98
98

23
14

23

23

45

45

23
23

14

45

14

45

98
23

14

14
45

14

67

33

67

67

45
98

Merge

67

42
33

42

33

42

98
98

98
98

23
14

23

23

45

45

23
23

14

45

14

45

98
23

14

14
45

14

45
98

67

33

67

67

6
Merge

67

42
33

42

33

42

98
98

98
98

23
14

23

23

45

45

23
23

14

45

14

45

98
23

14

14
45

14

45
98

67

33

67

67

67

67

Merge

42
33

42

33

42

98
98

98
98

23
14

23

23

45

45

23
23

14

45

14

45

98
23

14

14
45

14

45
98

67

33

67

67

67

67

42
33

42

33
33

42
42

98
98

98
98

23
14

23

23

45

45

23
23

14

45

14

45

98
23

14

14
45

14

45
98

67

33

67

67

67

42
33

42

33

42

33

67
Merge

42

98
98

98
98

23
14

23

23

45

45

23
23

14

45

14

45

98
23

14

14
45

14

45
98

67

33

67

67

67

67

42
33

42

33

42

33

33
Merge

42

98
98

98
98

23
14

23

23

45

45

23
23

14

45

14

45

98
23

14

14
45

14

45
98

67

33

67

67

67

67

42
33

42

33

42

33

33

42

42

Merge

98
98

98
98

23
14

23

23

45

45

23
23

14

45

14

45

98
23

14

14
45

14

45

67

33

67

67

67

67

42
33

42

33
33

33

98
Merge

42
42

42

98
98

98
98

23
14

23

23

45

45

23
23

14

45

14

45

98
23

14

14
45

14

45
98

67

33

67

67

67

67

42
33

42

33
33

33

6
Merge

42
42

42

98
98

98
98

23
14

23

23

45

45

23
23

14

45

14

45

98
23

14

14
45

14

45
98

67

33

67

67

67

6
6

67

42
33

42

33
33

33

33
Merge

42
42

42

98
98

98
98

23
14

23

23

45

45

23
23

14

45

14

45

98
23

14

14
45

14

45
98

67

33

67

67

42
33

33

67

6
6

33

67
33

42

33
42
Merge

42
42

42

98
98

98
98

23
14

23

23

45

45

23
23

14

45

14

45

98
23

14

14
45

14

45
98

67

33

67

67

42
33

33

67

6
6

42

33

67
33

42

33
42
Merge

67

42

42

98
98

98
98

23
14

23

23

45

45

23
23

14

45

14
45

14

45

98
23

14

14

45

67
6

67

67

Merge

42
33

42

33

67
33

42

33

67

98

33

33
42

67

42

42

98
98

98
98

23
14

23

23

45

45

23
23

14

45

14
45

14

45

98
23

14

14

45

67
6

67

67

Merge

42
33

42

33

67
33

42

33

67

98

33

33
42

67

42

42

98
98

98
98

23
14

23

23

45

45

23

14

45

98

14
45

14

45

23

23

14

14

45

67
6

67

67

14

Merge

42
33

42

33

67
33

42

33

67

98

33

33
42

67

42

42

98
98

98
98

23
14

23

23

45

45

23

14

45

98

14

14
45
14

14

45

23

23

14

45

67
6

67

67

23

Merge

42
33

42

33

67
33

42

33

67

98

33

33
42

67

42

42

98
98

98
98

23
14

23

23

45

45

23

14

45

98

14

14
45
14

14

45

23

23

14

45

67

67

Merge

42
33

42

33

67
33

42

33

67

6
33

33

98
23

67

33
42

67

42

42

98
98

98
98

23
14

23

23

45

45

23

14

45

98

14

14
45
14

14

45

23

23

14

67

67

6
33

42

Merge

42
33

42

33

67
33

42

33

67

98

33

45

23

67

33
42

67

42

42

98
98

98
98

23
14

23

23

45

45

23

14

45

98

14

14
45
14

14

45

23

23

14

67

67

42

Merge

33

45

33

42

33

42

33

67

6
33

42

67

98

33

45

23

67

33
42

67

42

42

98
98

98
98

23
14

23

23

45

45

23

14

45

98

14

14
45
14

14

45

23

23

14

67

67

42

Merge

33

45

42

33

42

33

67

6
33

42

67

98

33

45

23

67

33

33
67

42

67

42

42

98
98

98
98

23
14

23

23

45

45

23

14

45

98

14

14
45
14

14

45

23

23

14

67

67

42

Merge

33

42

33

45

42

33

67

6
33

42

67

98

33

45

23

67

33

33
67

42
98

67

42

42

98
98

98
98

23
14

23

23

45

45

23

14

45

98

14

14
45
14

14

45

23

23

14

67

67

42

33

42

33

45

42

33

67

6
33

42

67

98

33

45

23

67

33

33
67

42
98

67

42

42

98

23

45

14

67

33

42

14

23

33

42

45

67

98

Heap Sort
It is an improved version of selection sort in
which the largest element is selected and
exchanged with the last element in the
unsorted list.

set index to 1
loop (heap built)
reheapUp (heap, index)
increment index
end loop
heap created. Now sort it.
set sorted to last
loop (until all data sorted)
exchange (heap, 0, sorted)
decrement sorted
reheapDown (heap, 0, sorted)
end loop
End heapSort

Original list

23

78

45

32

56

23

45

23

78

Unsorted

After heap
After pass 1
and reheap
After pass 2
and reheap
After pass 3
and reheap
After pass 4
and reheap
After pass 5
and reheap

After pass 6
and reheap

78

32

56
Heap

56

32

45

Sorted

Heap

45

32

23

56

Heap

32

23

Sorted

45

Heap

23

32

Heap

56

78

Sorted

45

56

78

Sorted

23

Heap

78

32

45

56

78

56

78

Sorted

23

32

45

Sorted

Heap Sort: Summary


Heap sort uses a heap data structure to
improve selection sort and make the running
time asymptotically optimal
Running time is O(n log n) like merge sort,
but unlike selection, insertion, or bubble sorts
Sorts in place like insertion, selection or
bubble sorts, but unlike merge sort

S-ar putea să vă placă și