forked from pclubuiet/github101
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add c program to sort integers pclubuiet#109
- Loading branch information
Showing
1 changed file
with
34 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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]); | ||
} |