0% found this document useful (0 votes)
5 views2 pages

Anselsort

Vbvczvkgdjgxjfzjgxkgxkgx

Uploaded by

kpagarg4392
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)
5 views2 pages

Anselsort

Vbvczvkgdjgxjfzjgxkgxkgx

Uploaded by

kpagarg4392
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/ 2

Aim: To study about Selection sort and its complexity analysis

Selection Sort
In selection sort, the smallest value among the unsorted elements of the array is selected in
every pass and inserted to its appropriate position into the array. It is also the simplest
algorithm. It is an in-place comparison sorting algorithm. In this algorithm, the array is
divided into two parts, first is sorted part, and another one is the unsorted part. Initially,
the sorted part of the array is empty, and unsorted part is the given array. Sorted part is
placed at the left, while the unsorted part is placed at the right.
Algorithm

SELECTION SORT(arr, n)
Step 1: Repeat Steps 2 and 3 for i = 0 to n-1
Step 2: CALL SMALLEST(arr, i, n, pos)
Step 3: SWAP arr[i] with arr[pos]
[END OF LOOP]
Step 4: EXIT
SMALLEST (arr, i, n, pos)
Step 1: [INITIALIZE] SET SMALL = arr[i]
Step 2: [INITIALIZE] SET pos = i
Step 3: Repeat for j = i+1 to n
if (SMALL > arr[j])
SET SMALL = arr[j]
SET pos = j
[END OF if] Working
[END OF LOOP]
Step 4: RETURN pos
Complexity of Selection Sort
o Best Case Complexity - It occurs when there is no sorting required, i.e. the array is
already sorted. The best-case time complexity of selection sort is O(n2).
o Average Case Complexity - It occurs when the array elements are in jumbled order
that is not properly ascending and not properly descending. The average case time
complexity of selection sort is O(n2).
o Worst Case Complexity - It occurs when the array elements are required to be sorted
in reverse order. That means suppose you have to sort the array elements in ascending
order, but its elements are in descending order. The worst-case time complexity of
selection sort is O(n2).

Space Complexity

The space complexity of selection sort is O(1). It is because, in selection sort, an extra variable
is required for swapping.

Program

#include <iostream>
using namespace std;
void selection(int arr[], int n)
{
int i, j, small;
for (i = 0; i < n-1; i++)
{
small = i; int main()
for (j = i+1; j < n; j++) {
if (arr[j] < arr[small]) int a[] = { 80, 10, 29, 11, 8, 30, 15
small = j; };
int temp = arr[small]; int n = sizeof(a) / sizeof(a[0]);
arr[small] = arr[i]; cout<< "Before sorting array ele
arr[i] = temp; ments are - "<<endl;
} printArr(a, n);
} selection(a, n);
void printArr(int a[], int n) cout<< "\nAfter sorting array ele
{ ments are - "<<endl;
int i; printArr(a, n);
for (i = 0; i < n; i++) return 0;
cout<< a[i] <<" "; }
}

You might also like