Skip to content

Commit

Permalink
add c program to sort integers pclubuiet#109
Browse files Browse the repository at this point in the history
  • Loading branch information
archu5 committed Oct 27, 2018
1 parent 80dd25b commit b447af8
Showing 1 changed file with 34 additions and 0 deletions.
34 changes: 34 additions & 0 deletions programs/C/sort.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
#include < stdio.h >

int main() {
int temp, arr[1000], n, opt;
printf("please enter how many integers you want to sort: "); //prompt user for how many numbers user want to sort
scanf("%d", & n);
printf("options: 1.ascending order\n2.descending order\nenter option no:");
scanf("%d", & opt);
printf("enter numbers to sort:"); //prompt user to select from above options
for (int i = 0; i < n; i++) {
scanf("%d", & arr[i]);
}
for (int j = 0; j < n; j++) {
for (int k = j + 1; k < n; k++) {
if (opt == 1) {
if (arr[j] > arr[k]) { //swap higher integer with lower one to sort in ascending order
temp = arr[j];
arr[j] = arr[k];
arr[k] = temp;
}
}
if (opt == 2) {
if (arr[j] < arr[k]) { // swap lower integer with higher one to sort in descending order
temp = arr[j];
arr[j] = arr[k];
arr[k] = temp;

}
}
}
}
for (int i = 0; i < n; i++)
printf("%d ", arr[i]);
}

0 comments on commit b447af8

Please sign in to comment.