 Design and Analysis of Algorithms
 Home
 Basics of Algorithms
 DAA  Introduction
 DAA  Analysis of Algorithms
 DAA  Methodology of Analysis
 Asymptotic Notations & Apriori Analysis
 DAA  Space Complexities
 Design Strategies
 DAA  Divide & Conquer
 DAA  MaxMin Problem
 DAA  Merge Sort
 DAA  Binary Search
 Strassen’s Matrix Multiplication
 DAA  Greedy Method
 DAA  Fractional Knapsack
 DAA  Job Sequencing with Deadline
 DAA  Optimal Merge Pattern
 DAA  Dynamic Programming
 DAA  01 Knapsack
 Longest Common Subsequence
 Graph Theory
 DAA  Spanning Tree
 DAA  Shortest Paths
 DAA  Multistage Graph
 Travelling Salesman Problem
 Optimal Cost Binary Search Trees
 Heap Algorithms
 DAA  Binary Heap
 DAA  Insert Method
 DAA  Heapify Method
 DAA  Extract Method
 Sorting Methods
 DAA  Bubble Sort
 DAA  Insertion Sort
 DAA  Selection Sort
 DAA  Quick Sort
 DAA  Radix Sort
 Complexity Theory
 Deterministic vs. Nondeterministic Computations
 DAA  Max Cliques
 DAA  Vertex Cover
 DAA  P and NP Class
 DAA  Cook’s Theorem
 NP Hard & NPComplete Classes
 DAA  Hill Climbing Algorithm
 DAA Useful Resources
 DAA  Quick Guide
 DAA  Useful Resources
 DAA  Discussion
 Selected Reading
 UPSC IAS Exams Notes
 Developer's Best Practices
 Questions and Answers
 Effective Resume Writing
 HR Interview Questions
 Computer Glossary
 Who is Who
Design and Analysis Selection Sort
This type of sorting is called Selection Sort as it works by repeatedly sorting elements. It works as follows: first find the smallest in the array and exchange it with the element in the first position, then find the second smallest element and exchange it with the element in the second position, and continue in this way until the entire array is sorted.
Algorithm: SelectionSort (A) fori ← 1 to n1 do min j ← i; min x ← A[i] for j ←i + 1 to n do if A[j] < min x then min j ← j min x ← A[j] A[min j] ← A [i] A[i] ← min x
Selection sort is among the simplest of sorting techniques and it works very well for small files. It has a quite important application as each item is actually moved at the most once.
Section sort is a method of choice for sorting files with very large objects (records) and small keys. The worst case occurs if the array is already sorted in a descending order and we want to sort them in an ascending order.
Nonetheless, the time required by selection sort algorithm is not very sensitive to the original order of the array to be sorted: the test if A[j] < min x is executed exactly the same number of times in every case.
Selection sort spends most of its time trying to find the minimum element in the unsorted part of the array. It clearly shows the similarity between Selection sort and Bubble sort.
Bubble sort selects the maximum remaining elements at each stage, but wastes some effort imparting some order to an unsorted part of the array.
Selection sort is quadratic in both the worst and the average case, and requires no extra memory.
For each i from 1 to n  1, there is one exchange and n  i comparisons, so there is a total of n  1 exchanges and
(n − 1) + (n − 2) + ...+ 2 + 1 = n(n − 1)/2 comparisons.
These observations hold, no matter what the input data is.
In the worst case, this could be quadratic, but in the average case, this quantity is O(n log n). It implies that the running time of Selection sort is quite insensitive to the input.
Implementation
Void SelectionSort(int numbers[], int array_size) { int i, j; int min, temp; for (i = 0; I < array_size1; i++) { min = i; for (j = i+1; j < array_size; j++) if (numbers[j] < numbers[min]) min = j; temp = numbers[i]; numbers[i] = numbers[min]; numbers[min] = temp; } }
Example
Unsorted list: 

1^{st} iteration:
Smallest = 5
2 < 5, smallest = 2
1 < 2, smallest = 1
4 > 1, smallest = 1
3 > 1, smallest = 1
Swap 5 and 1 

2^{nd} iteration:
Smallest = 2
2 < 5, smallest = 2
2 < 4, smallest = 2
2 < 3, smallest = 2
No Swap 

3^{rd} iteration:
Smallest = 5
4 < 5, smallest = 4
3 < 4, smallest = 3
Swap 5 and 3 

4^{th} iteration:
Smallest = 4
4 < 5, smallest = 4
No Swap 

Finally,
the sorted list is 
