0% found this document useful (0 votes)
5 views

Sorting_2

Uploaded by

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

Sorting_2

Uploaded by

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

Selection Sort

Insertion Sort
Selection Sort
The selection sort algorithm sorts an array by repeatedly finding the
minimum element (considering ascending order) from unsorted part
and putting it at the beginning. The algorithm maintains two subarrays
in a given array.
1) The subarray which is already sorted.
2) Remaining subarray which is unsorted.

In every iteration of selection sort, the minimum element (considering


ascending order) from the unsorted subarray is picked and moved to
the sorted subarray.
arr[] = 64 25 12 22 11 Step 3: Find the minimum element
Step 1: Find the minimum in arr[2...4] and place it at
element in arr[0...4] and place it beginning of arr[2...4]
at beginning 11 12 22 25 64
11 25 12 22 64
Step 4: Find the minimum element
Step 2: Find the minimum in arr[3...4] and place it at
element in arr[1...4] and place it beginning of arr[3...4]
at beginning of arr[1...4] 11 12 22 25 64
11 12 25 22 64
#include <stdio.h>
/* Function to print an array */
void swap(int *xp, int *yp) void printArray(int arr[], int size)
{ {
int temp = *xp; int i;
*xp = *yp; for (i=0; i < size; i++)
*yp = temp; printf("%d ", arr[i]);
} printf("\n");
}
void selectionSort(int arr[], int n)
{ // Driver program to test above functions
int i, j, min_idx; int main()
{
// One by one move boundary of unsorted int arr[] = {64, 25, 12, 22, 11};
subarray int n = sizeof(arr)/sizeof(arr[0]);
for (i = 0; i < n-1; i++) selectionSort(arr, n);
{ printf("Sorted array: \n");
// Find the minimum element in unsorted printArray(arr, n);
array return 0;
min_idx = i; }
for (j = i+1; j < n; j++)
if (arr[j] < arr[min_idx])
min_idx = j;

//Swap the found minimum element with the first


element
swap(&arr[min_idx], &arr[i]);
}
}
Selection Sort
Time Complexity: O(n2) as there are two nested loops.

1. Consider a situation where swap operation is very costly. Which of the following
sorting algorithms should be preferred so that the number of swap operations are
minimized in general?->SELECTION
2. Which sorting algorithm will take least time when all elements of input array are
identical? Consider typical implementations of sorting algorithms.->INSERTION
3. Which of the following sorting algorithms has the lowest worst-case complexity?-
>MERGE
4. https://www.geeksforgeeks.org/time-and-space-complexity-analysis-of-merge-
sort/
Insertion Sort
Insertion Sort
#include <stdio.h>
#include <math.h> // A utility function to print an array
of
/* Function to sort an array using insertion size n
sort*/ void printArray(int arr[], int n)
void insertionSort(int arr[], int n) {
{ int i;
int i, key, j; for (i=0; i < n; i++)
for (i = 1; i < n; i++) printf("%d ", arr[i]);
{ printf("\n");
key = arr[i]; }
j = i-1;

/* Move elements of arr[0..i-1], that


are /* Driver program to test insertion
greater than key, to one position sort */
ahead int main()
of their current position */ {
while (j >= 0 && arr[j] > key) int arr[] = {12, 11, 13, 5, 6};
{ int n = sizeof(arr)/sizeof(arr[0]);
arr[j+1] = arr[j];
j = j-1; insertionSort(arr, n);
} printArray(arr, n);
arr[j+1] = key;
return 0;
}
}
}
Insertion Sort
Time Complexity: O(n*n)

Boundary Cases: Insertion sort takes maximum time to sort if elements


are sorted in reverse order. And it takes minimum time (Order of n)
when elements are already sorted.

Uses: Insertion sort is used when number of elements is small. It can


also be useful when input array is almost sorted, only few elements are
misplaced in complete big array.
Thank
You

You might also like