Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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;
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please correct the indentation, Match Opening braces with the closing braces.

}
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]);
}