Answers for "sort array in ascending order in c"

C
0

sorting array in c

#include <stdio.h>

int main()
{
    int ara[10]={4,45,23,53,2,64,56,34,77,98};
    int i,j,temp;
    for(i = 0; i < 10; i++){
        
        for(j = 0; j< 10; j++){
            if(ara[i] < ara[j]){ //use getter than (>) symbol for sorting from getter than to less than
                
                temp = ara[i];
                ara[i] = ara[j];
                ara[j] = temp;
                
            }
        }
        
    }
    
    for(i = 0; i < 10; i++){ //printing array sorted array elements
        
        printf("%dt",ara[i]);
    }
    return 0;
}
Posted by: Guest on July-13-2021
-1

sorting array in c

#include <stdio.h>
    void main()
    {
 
        int i, j, a, n, number[30];
        printf("Enter the value of N n");
        scanf("%d", &n);
 
        printf("Enter the numbers n");
        for (i = 0; i < n; ++i)
            scanf("%d", &number[i]);
 
        for (i = 0; i < n; ++i) 
        {
 
            for (j = i + 1; j < n; ++j)
            {
 
                if (number[i] > number[j]) 
                {
 
                    a =  number[i];
                    number[i] = number[j];
                    number[j] = a;
 
                }
 
            }
 
        }
 
        printf("The numbers arranged in ascending order are given below n");
        for (i = 0; i < n; ++i)
            printf("%dn", number[i]);
 }
Posted by: Guest on September-19-2020
0

c program to find sot array in ascending order

// C program to sort the array in an
// ascending order using selection sort
 
#include <stdio.h>
#include<conio.h>
void swap(int* xp, int* yp)
{
    int temp = *xp;
    *xp = *yp;
    *yp = temp;
}
 
// Function to perform Selection Sort
void selectionSort(int arr[], int n)
{
    int i, j, min_idx;
 
    // One by one move boundary of unsorted subarray
    for (i = 0; i < n - 1; i++) {
 
        // Find the minimum element in unsorted array
        min_idx = i;
        for (j = i + 1; j < n; j++)
            if (arr[j] < arr[min_idx])
                min_idx = j;
 
        // Swap the found minimum element
        // with the first element
        swap(&arr[min_idx], &arr[i]);
    }
}
 
// Function to print an array
void printArray(int arr[], int size)
{
    int i;
    for (i = 0; i < size; i++)
        printf("%d ", arr[i]);
    printf("n");
}
void main()
{
    int arr[] = { 0, 23, 14, 12, 9 };
    int n = sizeof(arr) / sizeof(arr[0]);
    printf("Original array: n");
    printArray(arr, n);
 
    selectionSort(arr, n);
    printf("nSorted array in Ascending order: n");
    printArray(arr, n);
 
    getch();
}
Posted by: Guest on September-26-2021

Code answers related to "sort array in ascending order in c"

Code answers related to "C"

Browse Popular Code Answers by Language