Thanks to visit codestin.com
Credit goes to www.scribd.com

0% found this document useful (0 votes)
7 views32 pages

Sorting

The document discusses sorting algorithms, outlining the sorting problem and properties of sorting algorithms such as time complexity, space complexity, and stability. It details general-purpose sorting algorithms including selection sort, bubble sort, and merge sort, providing examples and their respective complexities. Key characteristics of these algorithms are highlighted, including their performance and stability in sorting orderable items.

Uploaded by

nmnq2015
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views32 pages

Sorting

The document discusses sorting algorithms, outlining the sorting problem and properties of sorting algorithms such as time complexity, space complexity, and stability. It details general-purpose sorting algorithms including selection sort, bubble sort, and merge sort, providing examples and their respective complexities. Key characteristics of these algorithms are highlighted, including their performance and stability in sorting orderable items.

Uploaded by

nmnq2015
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 32

Sorting

CSC 212: Data Structures

King Saud University


Outline

The sorting problem.


Properties of sorting algorithms.
General purpose sorting algorithms:
Quadratic sorting algorithms: selection sort and bubble sort.
Sub-quadratic sorting algorithms: merge sort.

(KSU) Sorting 2 / 16
The sorting problem

Given a list of n totally orderable items, rearrange the list in increasing (or decreasing) order.
Totally orderable means that any two items can be compared: numbers, characters,
strings etc.
The items are usually called keys.

(KSU) Sorting 3 / 16
The sorting problem

Given a list of n totally orderable items, rearrange the list in increasing (or decreasing) order.
Totally orderable means that any two items can be compared: numbers, characters,
strings etc.
The items are usually called keys.

Example
This is an instance of the sorting problem:

(12, 5, 8, 16, 9, 31) → (5, 8, 9, 12, 16, 31) .

(KSU) Sorting 3 / 16
The sorting problem

Given a list of n totally orderable items, rearrange the list in increasing (or decreasing) order.
Totally orderable means that any two items can be compared: numbers, characters,
strings etc.
The items are usually called keys.

Example
This is an instance of the sorting problem:

(12, 5, 8, 16, 9, 31) → (5, 8, 9, 12, 16, 31) .

Often, the elements to be sorted contain keys and data:

((5, A), (2, C), (7, A), (2, B), (1, B)) → ((1, B), (2, C), (2, B), (5, A), (7, A)) .

(KSU) Sorting 3 / 16
Properties of sorting algorithms
Time complexity: worst case and average case.

(KSU) Sorting 4 / 16
Properties of sorting algorithms
Time complexity: worst case and average case.
Space complexity: the amount of extra space needed by the algorithm.

(KSU) Sorting 4 / 16
Properties of sorting algorithms
Time complexity: worst case and average case.
Space complexity: the amount of extra space needed by the algorithm.
Definition
An algorithm is said in-place if it does not require more than O(log n) space in addition to the
input.

(KSU) Sorting 4 / 16
Properties of sorting algorithms
Time complexity: worst case and average case.
Space complexity: the amount of extra space needed by the algorithm.
Definition
An algorithm is said in-place if it does not require more than O(log n) space in addition to the
input.

Stability: A sorting algorithm is stable if it does not change the order of equal elements.

(KSU) Sorting 4 / 16
Properties of sorting algorithms
Time complexity: worst case and average case.
Space complexity: the amount of extra space needed by the algorithm.
Definition
An algorithm is said in-place if it does not require more than O(log n) space in addition to the
input.

Stability: A sorting algorithm is stable if it does not change the order of equal elements.
Example
Given the input array: {(5, A), (2, C), (7, A), (2, B), (1, B)}, where we want to sort according
to the first element of the pairs (the integers), then:

(KSU) Sorting 4 / 16
Properties of sorting algorithms
Time complexity: worst case and average case.
Space complexity: the amount of extra space needed by the algorithm.
Definition
An algorithm is said in-place if it does not require more than O(log n) space in addition to the
input.

Stability: A sorting algorithm is stable if it does not change the order of equal elements.
Example
Given the input array: {(5, A), (2, C), (7, A), (2, B), (1, B)}, where we want to sort according
to the first element of the pairs (the integers), then:
{(1, B), (2, C), (2, B), (5, A), (7, A)} is a stable sorting.

(KSU) Sorting 4 / 16
Properties of sorting algorithms
Time complexity: worst case and average case.
Space complexity: the amount of extra space needed by the algorithm.
Definition
An algorithm is said in-place if it does not require more than O(log n) space in addition to the
input.

Stability: A sorting algorithm is stable if it does not change the order of equal elements.
Example
Given the input array: {(5, A), (2, C), (7, A), (2, B), (1, B)}, where we want to sort according
to the first element of the pairs (the integers), then:
{(1, B), (2, C), (2, B), (5, A), (7, A)} is a stable sorting.
{(1, B), (2, B), (2, C), (5, A), (7, A)} is not a stable sorting, since the order of (2, B) and (2, C) is
reversed.
(KSU) Sorting 4 / 16
General purpose sorting algorithms

Algorithms which can be used to sort any type of keys.


They are based on comparison only and do not assume any other property in the keys (for
example, they do not require the keys to be integers or strings).

(KSU) Sorting 5 / 16
Selection sort

Selection sort gradually builds the sorted array by finding the correct key for each new position.
p u b l i c s t a t i c v o i d selectionSort ( i n t [] A , i n t n ) {
f o r ( i n t i = 0; i < n - 1; i ++) {
i n t min = i ;
f o r ( i n t j = i + 1; j < n ; j ++) { // Search for the minimum
i f ( A [ j ] < A [ min ])
min = j ;
}
// Swap A [ i ] with A [ min ]
i n t tmp = A [ i ];
A [ i ] = A [ min ];
A [ min ] = tmp ;
}
}

(KSU) Sorting 6 / 16
Selection sort

Example
⇑ indicates i, ↑ indicates min.

 
12, 5, 8, 16, 9, 31
⇑ ↑

(KSU) Sorting 7 / 16
Selection sort

Example
⇑ indicates i, ↑ indicates min.

 
12, 5, 8, 16, 9, 31
⇑ ↑
 
5, 12, 8, 16, 9, 31
⇑ ↑

(KSU) Sorting 7 / 16
Selection sort

Example
⇑ indicates i, ↑ indicates min.

 
12, 5, 8, 16, 9, 31
⇑ ↑
 
5, 12, 8, 16, 9, 31
⇑ ↑
 
5, 8, 12, 16, 9, 31
⇑ ↑

(KSU) Sorting 7 / 16
Selection sort

Example
⇑ indicates i, ↑ indicates min.

   
12, 5, 8, 16, 9, 31 5, 8, 9, 16, 12, 31
⇑ ↑ ⇑ ↑
 
5, 12, 8, 16, 9, 31
⇑ ↑
 
5, 8, 12, 16, 9, 31
⇑ ↑

(KSU) Sorting 7 / 16
Selection sort

Example
⇑ indicates i, ↑ indicates min.

   
12, 5, 8, 16, 9, 31 5, 8, 9, 16, 12, 31
⇑ ↑ ⇑ ↑
   
5, 12, 8, 16, 9, 31 5, 8, 9, 12, 16, 31
⇑ ↑ ⇑
 
5, 8, 12, 16, 9, 31
⇑ ↑

(KSU) Sorting 7 / 16
Selection sort

Worst case time complexity: O(n2 ) (quadratic).


Average case time complexity: O(n2 ).
Space complexity: O(1).
In-place: Yes.
Stable: No.
Example
The array {(2, A), (2, B), (1, C)} will be sorted as {(1, C), (2, B), (2, A)}.

(KSU) Sorting 8 / 16
Bubble sort

Bubble sort sorts the array by repeatedly swapping non-ordered adjacent keys. After each for
loop iteration, the maximum is moved (or bubbled) towards the end.
p u b l i c s t a t i c v o i d bubbleSort ( i n t A [] , i n t n ) {
f o r ( i n t i = 0; i < n - 1; i ++) {
f o r ( i n t j = 0; j < n - 1 - i ; j ++) {
i f ( A [ j ] > A [ j + 1]) {
// Swap A [ j ] with A [ j + 1]
i n t tmp = A [ j ];
A [ j ] = A [ j + 1];
A [ j + 1] = tmp ;
}
}
}
}

(KSU) Sorting 9 / 16
Bubble sort

Example

(12, 5, 8, 16, 9, |31)

(KSU) Sorting 10 / 16
Bubble sort

Example

(12, 5, 8, 16, 9, |31)


(5, 8, 12, 9, |16, 31)

(KSU) Sorting 10 / 16
Bubble sort

Example

(12, 5, 8, 16, 9, |31)


(5, 8, 12, 9, |16, 31)
(5, 8, 9, |12, 16, 31)

(KSU) Sorting 10 / 16
Bubble sort

Example

(12, 5, 8, 16, 9, |31) (5, 8, |9, 12, 16, 31)


(5, 8, 12, 9, |16, 31)
(5, 8, 9, |12, 16, 31)

(KSU) Sorting 10 / 16
Bubble sort

Example

(12, 5, 8, 16, 9, |31) (5, 8, |9, 12, 16, 31)


(5, 8, 12, 9, |16, 31) (5, |8, 9, 12, 16, 31)
(5, 8, 9, |12, 16, 31)

(KSU) Sorting 10 / 16
Bubble sort

Worst case time complexity: O(n2 ) (quadratic).


Average case time complexity: O(n2 ).
Space complexity: O(1).
In-place: Yes.
Stable: Yes.
Remark
Bubble sort performs a lot of swaps, and as a result it has in practice a poor performance
compared to selection sort.

(KSU) Sorting 11 / 16
Merge sort

Merge sort is a divide-and-conquer algorithms to sort an array of n elements:


1 Divide the array into two equal parts.
2 Sort each part apart (recursively).
3 Merge the two sorted parts.
The key step in merge sort is merging two sorted arrays, which can be done in O(n).
Example
Given two arrays B = {1, 4, 6} and C = {2, 3, 7, 8}, the result of merging B and C is
{1, 2, 3, 4, 6, 7, 8}.

(KSU) Sorting 12 / 16
Merge sort

p u b l i c s t a t i c v o i d mergeSort ( i n t [] A , i n t l , i n t r ) {
i f ( l >= r )
return ;
i n t m = ( l + r ) / 2;
mergeSort (A , l , m ) ; // Sort first half
mergeSort (A , m + 1 , r ) ; // Sort second half
merge (A , l , m , r ) ; // Merge
}

(KSU) Sorting 13 / 16
Merge sort
p r i v a t e s t a t i c v o i d merge ( i n t [] A , i n t l , i n t m , i n t r ) {
i n t [] B = new i n t [ r - l + 1];
i n t i = l , j = m + 1 , k = 0;
w h i l e ( i <= m && j <= r )
i f ( A [ i ] <= A [ j ])
B [ k ++] = A [ i ++];
else
B [ k ++] = A [ j ++];
i f (i > m)
w h i l e ( j <= r )
B [ k ++] = A [ j ++];
else
w h i l e ( i <= m )
B [ k ++] = A [ i ++];
f o r ( k = 0; k < B . length ; k ++)
A [ k + l ] = B [ k ];
}

(KSU) Sorting 14 / 16
Merge sort
Example
Sort the array: 8, 3, 2, 9, 7, 1, 5, 4.

(KSU) Sorting 15 / 16
Merge sort

Worst case time complexity: O(n log n) (sub-quadratic).


Average case time complexity: O(n log n).
Space complexity: O(n) (requires auxiliary memory).
In-place: No.
Stable: Yes.

(KSU) Sorting 16 / 16

You might also like