Data Structures
Sorting
8 20 8 5 10 7
Turkan Ahmed
Computer Eng. Dept. , Collage of Eng., University of Mosul
2
What is Sorting?
Sorting: an operation that segregates items
into groups according to specified criterion.
A={3162134590}
A={0112334569}
• Internal Sort
– The data to be sorted is all stored in the computer’s
main memory.
• External Sort
– Some of the data to be sorted might be stored in some
external, slower, device.
Why Sort and Examples
3
Consider:
● Sorting Books in Library (Dewey system)
● Sorting Individuals by Height (Feet and Inches)
● Sorting Movies in Blockbuster (Alphabetical)
● Sorting Numbers (Sequential)
General Sorting
4
Assumptions
data in linear data structure
availability of comparator for elements
availability of swap routine (or shift )
no knowledge about the data values
Swap (in an Array)
5
public static void swap
(int data[], int i, int j)
{
int temp = data[i];
data[i] = data[j];
data[j] = temp;
}
Types of Sorting Algorithms
6
There are many, many different types of sorting
algorithms, but the primary ones are:
Simple Sort Algorithms
● Bubble Sort Complex Sort Algorithms
● Selection Sort
● Quick Sort
● Insertion Sort
● Radix Sort
● Swap Sort
●Merge Sort
● Shell Sort
● Heap Sort
Bubble Sort
7
• list[0]...list[n - 1]
– List of n elements, indexed 0 to n - 1
Bubble Sort
8
• Series of n - 1 iterations
– Successive elements list[index] and
list[index + 1] of list are compared
– If list[index] > list[index + 1]
• Elements list[index] and list[index + 1] are
swapped
• Smaller elements move toward the top
• Larger elements move toward the bottom
Bubble Sort
9
Bubble Sort
10
Bubble Sort
11
Bubble Sort
12
Bubble Sort Example
13
Start – Unsorted
Compare, swap (0, 1)
Compare, swap (1, 2)
Compare, no swap
Compare, noswap
Compare, swap (4, 5)
99 in position
Bubble Sort Example
14
Pass 2
swap (0, 1)
no swap
no swap
swap (3, 4)
21 in position
Bubble Sort Example
15
Pass 3
no swap
no swap
swap (2, 3)
12 in position, Pass 4
no swap
swap (1, 2)
8 in position, Pass 5
swap (1, 2)
Done
Bubble Sort Algorithm
16
Bubble Sort – Algorithm Complexity
17
Time consuming operations
compares, swaps.
#Compares
a for loop embedded inside a while loop
(n-1)+(n-2)+(n-3) …+1 , or O(n2)
#Swaps
inside a conditional -> #swaps data dependent !!
Best Case 0, or O(1)
Worst Case (n-1)+(n-2)+(n-3) …+1 , or O(n2)
Space
size of the array
an in-place algorithm
18
Smart Bubble Sort
Simplest sorting algorithm
Idea:
1. Set flag = false
2. Traverse the array and compare pairs of two
elements
1.1 If E1 E2 - OK
1.2 If E1 > E2 then Switch(E1, E2) and set flag = true
3. If flag = true goto 1.
What happens?
Smart Bubble Sort Algorithm
19
void bubbleSort (Array S, length n) {
boolean isSorted = false;
while(!isSorted) {
isSorted = true;
for(i = 0; i<n; i++) {
if(S[i] > S[i+1]) {
int aux = S[i];
S[i] = S[i+1];
S[i+1] = aux;
isSorted = false;
}
}
}
Bubble Sort Function
20
// This function is the "dumb" Bubble Sort, which continues to
// compare array elements after the data is already sorted.
void BubbleSort1(int Array[],int N)
{
int P,Q,T;
for (P = 1; P < N; P++)
{
for (Q = 0; Q < N-P; Q++)
if (Array[Q]>Array[Q+1])
{
T = Array[Q]; Array[Q] = Array[Q+1]; Array[Q+1] = T;
}
for (Q = 0; Q < N; Q++)
cout<<Array[Q]<<" ";
cout<<endl;
}
}
Smart Bubble Sort Function
21
// This function is the "smart" Bubble Sort, which stops to check
// if array elements need to be swapped after the list is already sorted.
void BubbleSort2(int Array[],int N)
{ int Q,T;
int Sorted=0;
int P = 0;
do {
Sorted = 1;
P++;
for (Q = 0; Q < N-P; Q++)
if (Array[Q] > Array[Q+1])
{ Sorted = 0;
T = Array[Q];
Array[Q] = Array[Q+1];
Array[Q+1] = T; }
} while (!Sorted);
}
22
Sorting in General
The efficiency of our algorithms is
dependent on the
number of comparisons we have to make with
our keys.
In addition, we will learn that sorting will also
depend on how frequently we must move
items around.
Selection Sort
23
• Idea:
– Find the smallest element in the array
– Exchange it with the element in the first position
– Find the second smallest element and exchange it with
the element in the second position
– Continue until the array is sorted
• Disadvantage:
– Running time depends only slightly on the amount of order
in the file
Selection Sort
24
Rearrange the list by selecting an element in the list and moving it to its
proper position
Finds the location of the smallest element in the unsorted portion of the list
Moves it to the top of the unsorted portion of the list
Selection Sort
25
Selection Sort
26
Selection Sort
27
5 5 5 5 5 5 5
7 7 7 7 7 7 7
24 16 16 16 16 16 16
30 30 24 24 24 24 24
62 62 62 30 30 30 30
45 45 45 45 45 45 45
16 24 30 62 62 62 55
55 55 55 55 55 55 62
Selection Sort
28
• In the unsorted portion of the list:
– Find the location of the smallest element
– Move the smallest element to the beginning of the
unsorted list
Selection Sort Algorithm
29
Selection Sorting (Example)
30
Selection Sorting Function
31
// The Selection Sort function uses the same principle as the
// Bubble Sort. One major improvement is used by tracking the largest element during
// the comparison pass.
// The result is that the Selection Sort only makes one swap for each pass.
void SelectionSort(int Array[],int N)
{
int P,Q,T;
int Smallest;
for (P = 0; P < N; P++)
{
Smallest = P;
for (Q = P+1; Q < N; Q++)
if (Array[Q] < Array[Smallest])
Smallest = Q;
if (Array[P] != Array[Smallest])
{
T = Array[P];
Array[P] = Array[Smallest];
Array[Smallest] = T;
}
} }
Insertion Sort
32
Idea: like sorting a hand of playing cards
Start with an empty left hand and the cards facing down on
the table.
Remove one card at a time from the table, and insert it into
the correct position in the left hand
compare it with each of the cards already in the hand, from right
to left
The cards held in the left hand are sorted
these cards were originally the top cards of the pile on the table
Insertion Sort
To insert 12, we need to
make room for it by moving
first 36 and then 24.
33
Insertion Sort
34
Insertion Sort
35
Insertion Sort
36
input array
5 2 4 6 1 3
at each iteration, the array is divided in two sub-arrays:
left sub-array right sub-array
unsorted
sorted
36
Insertion Sort
37
37
38
Insertion Sort
Reduces number of key comparisons made
in selection sort
Can be applied to both arrays and linked
lists.
Sorts list by
Finding first unsorted element in list
Moving it to its proper position
Insertion Sort: Array-Based Lists
39
Insertion Sort: Array-Based Lists
40
Insertion Sort: Array-Based Lists
41
Insertion Sort: Array-Based Lists
42
Insertion Sort: Functions
43
// This Insertion Sort uses an array to store sorted data.
// A Linear Search function is used first to find the proper
// insertion index, followed by an InsertItem function that
// shifts the array elements and inserts the new number.
void LinearSearch(int Array[],int SearchNumber,int N, int
&Index)
{
Index = 0;
while (Index < N && SearchNumber > Array[Index])
Index++;
}
Insertion Sort: Functions
44
void InsertItem(int Array[],int SearchNumber,int N,int Index)
{
int K;
for (K = N-1; K > Index; K--)
Array[K] = Array[K-1];
Array[Index] = SearchNumber;
}
Insertion Sort: Functions
45
void InsertSort(int Array[],int N)
{ int Index,K;
int Size;
int Nr;
for (K = 1; K < N; K++)
{
Size = K + 1;
Nr=Array[K];
LinearSearch(Array,Nr,Size,Index);
InsertItem(Array,Nr,Size,Index);
} }
Comparison of Quadratic Sorts
46
Comparisons Exchanges
Best Worst Best Worst
Selection O(n²) O(n²) O(1) O(n)
Sort
Bubble O(n) O(n²) O(1) O(n²)
Sort
Insertion O(n) O(n²) O(1) O(n²)
Sort
Exercise
47
Why is the smart bubble sort called smart?
What is the sorting mechanism of the
bubble sort ?
What is the sorting mechanism of the
Selection sort ?
What is the sorting mechanism of the
Insertion sort ?