From f5a60f4b048c18aa6be0c0bde065a567d8a0c668 Mon Sep 17 00:00:00 2001 From: archit kaushal Date: Thu, 25 Oct 2018 21:32:13 +0530 Subject: [PATCH] add c program to sort integers --- 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..b1c7fd7 --- /dev/null +++ b/programs/C/sort.c @@ -0,0 +1,34 @@ +#include + +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