Sorting is a process of arranging numbers according to a certain
sequence. Write a C functions to read n integer values, sorting
process and display the arranged numbers.
#include <stdio.h>
#define N 100
void sort(int* arr, int n) {
int i, j, tmp;
for (i=0; i<n-1; i++) {
for (j=i+1; j<n; j++) {
if (arr[j] < arr[i]) {
tmp = arr[i];
arr[i] = arr[j];
arr[j] = tmp;
}
}
}
}
int main() {
int n, i;
int arr[N];
printf("Enter n: ");
scanf("%d", &n);
printf("Enter %d integers: ", n);
for (i=0; i<n; i++) {
scanf("%d", &arr[i]);
}
sort(arr, n);
printf("Sorted array:\n");
for (i=0; i<n; i++) {
printf("%d ", arr[i]);
}
return 0;
}
Comments
Leave a comment