From b447af8778e7889be8e839175927f902b012795c Mon Sep 17 00:00:00 2001 From: archit kaushal Date: Sat, 27 Oct 2018 13:15:01 +0530 Subject: [PATCH] add c program to sort integers #109 --- programs/C/sort.c | 34 ++++++++++++++++++++++++++++++++++ 1 file changed, 34 insertions(+) create mode 100644 programs/C/sort.c diff --git a/programs/C/sort.c b/programs/C/sort.c new file mode 100644 index 0000000..be08fa8 --- /dev/null +++ b/programs/C/sort.c @@ -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]); + } \ No newline at end of file