c program to find sot array in ascending order

Solutions on MaxInterview for c program to find sot array in ascending order by the best coders in the world

showing results for - "c program to find sot array in ascending order"
Isabel
10 Mar 2018
1// C program to sort the array in an
2// ascending order using selection sort
3 
4#include <stdio.h>
5#include<conio.h>
6void swap(int* xp, int* yp)
7{
8    int temp = *xp;
9    *xp = *yp;
10    *yp = temp;
11}
12 
13// Function to perform Selection Sort
14void selectionSort(int arr[], int n)
15{
16    int i, j, min_idx;
17 
18    // One by one move boundary of unsorted subarray
19    for (i = 0; i < n - 1; i++) {
20 
21        // Find the minimum element in unsorted array
22        min_idx = i;
23        for (j = i + 1; j < n; j++)
24            if (arr[j] < arr[min_idx])
25                min_idx = j;
26 
27        // Swap the found minimum element
28        // with the first element
29        swap(&arr[min_idx], &arr[i]);
30    }
31}
32 
33// Function to print an array
34void printArray(int arr[], int size)
35{
36    int i;
37    for (i = 0; i < size; i++)
38        printf("%d ", arr[i]);
39    printf("\n");
40}
41void main()
42{
43    int arr[] = { 0, 23, 14, 12, 9 };
44    int n = sizeof(arr) / sizeof(arr[0]);
45    printf("Original array: \n");
46    printArray(arr, n);
47 
48    selectionSort(arr, n);
49    printf("\nSorted array in Ascending order: \n");
50    printArray(arr, n);
51 
52    getch();
53}