void selectionSort(vector<int>& arr, int n) {
for(int i=0 ; i<= n-2 ; i++){
int min = i ;
for(int j=i ; j<=n-1 ; j++){
if(arr[j] < arr[min]){
min = j ;
}
}
swap(arr[i], arr[min]);
}
}
Type |
Complexity |
Time (Best) |
O(n²) |
Time (Worst) |
O(n²) |
Time (Average) |
O(n²) |
Space |
O(1) |
Why Is It Important?
- Performance Optimization:
- Efficient algorithms run faster and use fewer resources.
- Important in real-time systems, embedded devices, or high-traffic servers.
- Scalability:
- Helps ensure your code performs well not just with small inputs but also with large data sets.
- Example: Searching a name in a database of 1,000 vs. 10 million users.
- Resource Constraints:
- On limited systems (like mobile or IoT devices), memory and CPU usage are critical.
- Algorithm Comparison:
- Allows you to objectively compare two solutions and pick the best one.