Complexity : O(n2)
Explanation:
Below code picks a cell one by one and compares with each other cells , if value of current cell is greater than other cell then it will swap with other value. Similarly it performs untill picked cell is last cell.
Code :
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 |
#include<stdio.h> int main(){ int arr[100]; int size,i,j,temp; printf("n Enter the size of array to be taken input [max 100]: "); scanf("%d",&size); printf("n"); // A loop for input array for(i=0;i<size;i++){ printf("Enter the Element No %d : ",i+1); scanf("%d",&arr[i]); } // A loop for printing array printf("n Array Befor sortiong :n"); for(i=0;i<size;i++) printf(" %d ",arr[i]); // General sorting algorithm Complexity O(n^2) for(i=0;i<size;i++){ for(j=0;j<size;j++) { if(arr[i]<arr[j]) { temp=arr[i]; arr[i]=arr[j]; arr[j]=temp; } } } printf("n Array After sortiong :n"); for(i=0;i<size;i++) printf(" %d ",arr[i]); return 0; } |