Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1of 33

Data Structure and

Algorithm
Bilal Arif (Senior Lecturer)
Depart of Software Engineering, University of Management and
Technology Lahore.
Today
• Insertion sort
• Selection sort
• Bubble sort
What is sorting? and why do we need to sort data?

• Ordering data in an increasing or decreasing fashion according to


some linear relationship among the data items.
• Data sorting is any process that involves arranging the data into some
meaningful order to make it easier to search and analyze.
Insertion Sort
Insertion sort Criteria
Insert an element from unsorted array to its correct position in
sorted array.
Mechanism for Sorting in insertion Sort
0 1 2 3 4 5

12 45 23 51 19 8
Sorted Array Unsorted Array

Compare 45 unsorted array element with 12 sorted array


Mechanism for Sorting in insertion Sort
0 1 2 3 4 5

12 45 23 51 19 8
Sorted Array Unsorted Array

Compare 23 unsorted array element with 45 sorted array and onward


Mechanism for Sorting in insertion Sort
0 1 2 3 4 5

12 23 45 51 19 8
Sorted Array Unsorted Array

Compare 51 unsorted array element with 45 sorted array and onward


Mechanism for Sorting in insertion Sort
0 1 2 3 4 5

12 23 45 51 19 8
Sorted Array Unsorted Array

Compare 19 unsorted array element with 51 sorted array


Mechanism for Sorting in insertion Sort
0 1 2 3 4 5

12 23 45 19 51 8
Sorted Array Unsorted Array

Compare 19 unsorted array element with 45 sorted array


Mechanism for Sorting in insertion Sort
0 1 2 3 4 5

12 23 19 45 51 8
Sorted Array Unsorted Array

Compare 19 unsorted array element with 23 sorted array


Mechanism for Sorting in insertion Sort
0 1 2 3 4 5

12 19 23 45 51 8
Sorted Array Unsorted Array

Compare 19 unsorted array element with 12 sorted array


Mechanism for Sorting in insertion Sort
0 1 2 3 4 5

12 19 23 45 51 8
Sorted Array Unsorted Array

Compare 8 unsorted array element with 51 sorted array


Mechanism for Sorting in insertion Sort
0 1 2 3 4 5

12 19 23 45 8 51
Sorted Array

Compare 8 unsorted array element with 45 sorted array


Mechanism for Sorting in insertion Sort
0 1 2 3 4 5

12 19 23 8 45 51
Sorted Array

Compare 8 unsorted array element with 23 sorted array


Mechanism for Sorting in insertion Sort
0 1 2 3 4 5

12 19 8 23 45 51
Sorted Array

Compare 8 unsorted array element with 19 sorted array


Mechanism for Sorting in insertion Sort
0 1 2 3 4 5

12 8 19 23 45 51
Sorted Array

Compare 8 unsorted array element with 12 sorted array


Mechanism for Sorting in insertion Sort
0 1 2 3 4 5

8 12 19 23 45 51
Sorted Array

Sorted done 
Insertion Sort Code
Insertion Sort in C++ source Code
void insertionSort(){
for (int i = 1; i < size; i++)
{
int temp = arr[i];
int j = i - 1;
while(j >=0 && arr[j] > temp)
{
arr[j+1] = arr[j];
j--;
}
arr[j + 1] = temp;
}
}
Selection Sort
Selection Sort
Find the minimum element in unsorted array and swap it with
element at beginning.
Mechanism for Sorting in Selection Sort
0 1 2 3 4 5

12 45 23 51 19 8
Unsorted Array

Find the minimum element in unsorted array and swap at index 0?


Mechanism for Sorting in Selection Sort
0 1 2 3 4 5

8 45 23 51 19 12
Sorted Array Unsorted Array

Find the minimum element in unsorted array and swap at index 1?


Mechanism for Sorting in Selection Sort
0 1 2 3 4 5

8 12 23 51 19 45
Sorted Array Unsorted Array

Find the minimum element in unsorted array and swap at index 2?


Mechanism for Sorting in Selection Sort
0 1 2 3 4 5

8 12 19 51 23 45
Sorted Array Unsorted Array

Find the minimum element in unsorted array and swap at index 3?


Mechanism for Sorting in Selection Sort
0 1 2 3 4 5

8 12 19 23 51 45
Sorted Array Unsorted Array

Find the minimum element in unsorted array and swap at index 4?


Mechanism for Sorting in Selection Sort
0 1 2 3 4 5

8 12 19 23 45 51
Sorted Array Unsorted Array
Mechanism for Sorting in Selection Sort
0 1 2 3 4 5

8 12 19 23 45 51
Sorted Array

Now the array is fully sorted


Mechanism for Sorting in Selection Sort
0 1 2 3 4 5

8 12 19 51 23 45
Sorted Array Unsorted Array

Find the minimum element in unsorted array and swap at index 3?


Selection Sort Source Code in C++
void Selection_Sort() {
for (int i = 0; i < size - 1; i++){
int min = i;
for (int j = i + 1; j < size; j++){
if (arr[j] < arr[min])
min = j;
}
if (min != i){
int temp = arr[min];
arr[min] = arr[i];
arr[i] = temp;
}
}
}
Bubble Sort
Bubble Sort Source Code in C++
void Bubble_Sort(){
for (int i = 0; i < size; i++){
for (int j = 0; j < size-1; j++){
if (arr[j]>arr[j+1]){
int temp = arr[j];
arr[j] = arr[j + 1];
arr[j + 1] = temp;
}
}
}
}

You might also like