Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- int main(){
- int A[10];
- printf("Masukkan Elemen: \n");
- for(int i = 0; i < 10; i++) {
- scanf("%d", &A[i]);
- }
- int n = sizeof(A)/sizeof(A[0]);
- printf("\Before Sorting : \n");
- printArray(A, n);
- InsertionSortAsc(A, n);
- InsertionSortDesc(A, n);
- return 0;
- }
- void InsertionSortAsc(int A[], int n){
- int i, key, j;
- for(i=1; i<n; i++){
- key = A[i];
- j=i-1;
- while(j >= 0 && A[j]>key){
- A[j+1] = A[j];
- j=j-1;;
- }
- A[j+1] = key;
- }
- printf("\nAfter Sorting Asc : \n");
- printArray(A, n);
- }
- void InsertionSortDesc(int A[], int n){
- int i, key, j;
- for(i=1; i<n; i++){
- key = A[i];
- j=i-1;
- while(j >= 0 && A[j]<key){
- A[j+1] = A[j];
- j=j-1;;
- }
- A[j+1] = key;
- }
- printf("\nAfter Sorting Desc : \n");
- printArray(A, n);
- }
- void printArray(int A[], int n){
- int i;
- for(i=0; i<n; i++){
- printf("%d ", A[i]);
- }
- printf("\n");
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement