Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add c program to sort integers #109 #113

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
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]);
}