Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // Include Header files
- #include <stdio.h>
- // Declare some gloal variables
- int N = 7; // Size of list
- int arrayToSort[7];
- // Function to do shell sort
- int doSort(){
- // Some base variables
- int outer,inner;
- // Find the initial interval
- int interval = (N/2);
- // While loop will itter untill interval becomes 0
- while(interval > 0){
- printf("Interval Under Process : %d\n",interval);
- // This loop will check the element outer of the gap ie last element in gap
- for(outer = interval;outer<N;outer++){
- // Temp varaible to hold element at gap position
- int tempElement = arrayToSort[outer];
- inner = outer;
- // While works on some conditions
- // 1. Inner position must be greater then inteval position-1 because we want to check the
- // element present at gap
- // 2. if the outer element is smaller then the inner elemnt then swap
- while(inner > interval-1 && tempElement < arrayToSort[inner-interval]){
- arrayToSort[inner] = arrayToSort[inner-interval];
- // Decrease the inner position by interval
- inner = inner - interval;
- }
- // Swap the inner element also
- arrayToSort[inner] = tempElement;
- }
- interval = (interval/2);
- }
- printArray();
- }
- // Utility function to print array
- void printArray(){
- int i = 1;
- for(i = 0; i < N; i++){
- printf("%d ",arrayToSort[i]);
- }
- printf("\n");
- }
- int main(){
- int i = 0; // Var declaration for loop
- for(i=0;i<N;i++){
- printf("Enter Element For Position [%d] : ",i);
- scanf("%d",&arrayToSort[i]);
- }
- printArray();
- doSort();
- return 0;
- }
Add Comment
Please, Sign In to add comment