Untitled
unknown
plain_text
3 years ago
1.5 kB
8
Indexable
Best case Time complexity: O(n)
Worst case Time complexity: O(n^2)
Average Time complexity: O(n^2)
Space Complexity: O(1)
////////////////////////////////////////////////////////////////////////////////////////////////////////
here is the implementation of selection sort, with comments added for better understanding :o)
//////////////////////////////////////////////////////////////////////
// include all the necessary files..
#include<iostream>
using namespace std;
// from here, the execution of our program will start..
int main(){
// declare the variables
int i,j;
//declaring and initializing an unsorted/rotated array
int a[]={17,21,33,41,9,12};
// compute the size of the array..
int n=sizeof(a)/sizeof(a[0]);
// run a for loop from first element to the end of the array
for(i=1;i<n;i++){
// save the current element into a temporary variable named current...
int current=a[i];
// move the pointer 1 less position than the current
j=i-1;
// move the current into it's right position by comparing it with the values before this...
while(a[j]>current && j>=0){
a[j+1]=a[j];
j--;
}
// put the value of current in it's right position..
a[j+1]=current;
}
// run a loop in the sorted array and print the elements...
for(auto i:a){
cout<<i<<" ";
}
return(0);
}
//////////////////////////////////////////////////////////////////////Editor is loading...