0% found this document useful (0 votes)
18 views16 pages

Selection Sort-1

Selection sort works by iteratively finding the minimum element from unsorted part and putting it at the beginning. It divides array into sorted and unsorted part, initially unsorted part is the whole array and sorted part is empty. It picks the minimum element and puts it in sorted part in each iteration until whole array is sorted.

Uploaded by

farzadamini177
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)
18 views16 pages

Selection Sort-1

Selection sort works by iteratively finding the minimum element from unsorted part and putting it at the beginning. It divides array into sorted and unsorted part, initially unsorted part is the whole array and sorted part is empty. It picks the minimum element and puts it in sorted part in each iteration until whole array is sorted.

Uploaded by

farzadamini177
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/ 16

Selection Sort

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
Selection Sort

In selection sort, the first smallest element is selected from the unsorted
array and placed at the first position. After that second smallest element is
selected and placed in the second position. The process continues until the
array is entirely sorted.
Usage of Selection Sort
Selection sort is generally used when –

A small array is to be sorted


Swapping cost doesn't matter
It is compulsory to check all elements
Algorithm
Algorithm
Working Selection Sort Algorithm
Working Selection Sort Algorithm
Working Selection Sort Algorithm
So, swap 12 with 8. After the first iteration, 8 will appear at the first position in the sorted array.
Working Selection Sort Algorithm
Working Selection Sort Algorithm
Working Selection Sort Algorithm
Selection Sort Complexity
Implementation of Selection Sort
public class Selection {
void selection(int a[]) {
int i, j, small;
int n = a.length;
for (i = 0; i < n-1; i++) {
small = i;

for (j = i+1; j < n; j++)


if (a[j] < a[small])
small = j;
Implementation of Selection Sort
int temp = a[small];
a[small] = a[j];
a[j] = temp;
}
}
void printArr(int a[]) {
int i;
int n = a.length;
for (i = 0; i < n; i++)
System.out.print(a[i] + " ");
}
Implementation of Selection Sort
public static void main(String[] args) {
int a[] = { 91, 49, 4, 19, 10, 21 };
Selection i1 = new Selection();
System.out.println("\nBefore sorting array elements are - ");
i1.printArr(a);
i1.selection(a);
System.out.println("\nAfter sorting array elements are - ");
i1.printArr(a);
System.out.println(); }}

You might also like