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

Selection Sort

Uploaded by

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

Selection Sort

Uploaded by

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

~Selection sort~

Sorting refers to arranging the elements of a list into an order


of some kind.

-> A comparison based sorting algorithm runs the elements through


a single abstract comparison operation usually being the comparison operator.
Determines which of the two elements should occur first in the final sorted list.
For example: Bubble sort, insertion sort, merge sort,
heap sort, selection sort, quick sort.

-> A non comparison sort on the other hand is are algorithms which are not
comparison based.
For example: Counting sort and Radix Sort.

Sorting is crucial for efficient data retrieval and manipulation. It allows us to


quickly find items, analyse trends, and perform various operations more
effectively.

~Selection Sort~
Sorts the array by repeatedly finding the smallest element, it manages two sub
arrays one being the sorted array and one being the unsorted array to be sorted In
every iteration one unsorted element is moved to the sorted array.

-> Time complexity = O(n^2)


-> Algorithm paradigm = Divide and conquer.
-> Sorting in place = Yes

In laymen terms, Selection sort is an sorting algorithm which picks up the smallest
element from an array and swaps it with the unsorted element in the beginning.

APPLICATION:
-> Efficient for sorting small data sets.
-> Hardware level sorting.
-> Often used to teach sorting due to its simplicity.

CODE:

import java.util.*;

class SelectionSort{
static int[] sort(int arr[]){
int n = arr.length;

for(int i = 0; i < n - 1; i++){


int min_idx = i;

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


if(arr[min_idx] < arr[j]){
continue;
} else{
int temp = arr[j];
arr[j] = arr[min_idx];
arr[min_idx] = temp;
}
}
}
return arr;
}

static void printArray(int arr[]){


for(int i = 0; i < arr.length; i++){
System.out.printf("%d ", arr[i]);
}
}

public static void main(String[] args){


int[] arr = {12, 55, 1, 5, 6, 9, 12, 44};

arr = sort(arr);
printArray(arr);
}
}

You might also like