#include <stdio.h>
#include <stdlib.h>
#define INDEX 11
int main(){
const int index = 10;
int arr[INDEX] = {88, 55, 24, 1, 6, 35, 78, 56, 100, 11, 1};
for(int i = 0; i < INDEX; i++){
//traverse unsorted part
int temp = arr[i];
//compare with the latest sorted element
int j = i - 1;
// j = sorted indexs
//temp = element to put in sorted part
//repeat till sorted part index is smaller or same as i
while(temp < arr[j] && j >= 0){
//first j + 1 means arr[i]
//while excution means couldnt find right place
//thus sorted element part should move one cells to right side
arr[j + 1] = arr[j];
//reverse traversal
j--;
}
//when finally j + 1 is right place
//while finished -> should ++ to point proper j
//or no while excution -> it is in order => stay where u r
arr[j+1] = temp;
}
printf("The sorted array is:\n");
for(int i = 0; i < 11; i++){
printf("%d", arr[i]);
if(i != 10){
printf(" ");
}
else printf("\n");
}
return 0;
}
#include <stdlib.h>
#define INDEX 11
int main(){
const int index = 10;
int arr[INDEX] = {88, 55, 24, 1, 6, 35, 78, 56, 100, 11, 1};
for(int i = 0; i < INDEX; i++){
//traverse unsorted part
int temp = arr[i];
//compare with the latest sorted element
int j = i - 1;
// j = sorted indexs
//temp = element to put in sorted part
//repeat till sorted part index is smaller or same as i
while(temp < arr[j] && j >= 0){
//first j + 1 means arr[i]
//while excution means couldnt find right place
//thus sorted element part should move one cells to right side
arr[j + 1] = arr[j];
//reverse traversal
j--;
}
//when finally j + 1 is right place
//while finished -> should ++ to point proper j
//or no while excution -> it is in order => stay where u r
arr[j+1] = temp;
}
printf("The sorted array is:\n");
for(int i = 0; i < 11; i++){
printf("%d", arr[i]);
if(i != 10){
printf(" ");
}
else printf("\n");
}
return 0;
}
댓글
댓글 쓰기