Get free ebooK with 50 must do coding Question for Product Based Companies solved
Fill the details & get ebook over email
Thank You!
We have sent the Ebook on 50 Must Do Coding Questions for Product Based Companies Solved over your email. All the best!

Ascending Order Program in C

Last Updated on May 10, 2023 by Prepbytes

Ascending order refers to the arrangement of a set of numbers in increasing order, from the smallest value to the largest value. A program to sort numbers in ascending order in C can be useful for a variety of applications.

The process of sorting a set of numbers in ascending order can be done using different sorting algorithms such as bubble sort, insertion sort, selection sort, and more. In general, the sorting algorithm works by comparing pairs of adjacent elements and swapping them if they are in the wrong order.

Original array

5 2 8 7 1

Array after sorting

1 2 5 7 8

The order of the elements will be such that the smallest element, in this example 1, which is the smallest, will appear on the far left. On the far right, the greatest piece, in this case, 8 will be visible.

Logic Used to Sort an Array

The items may be sorted using a variety of reasoning. For the sake of simplicity, we will use a standard method used in ordinary living. In order to sort an array, we choose a certain element and position it correctly by comparing it to other components.

Explanation

Take input array ‘a’ and no of elements(n) as 4

Let us take elements for array a={7,8,12,3}.

1st iteration for(i=0;i<n;i++) i.e. for(i=0;0<4;i++)   – Outer for loop
1st iteration for(j=i+1;j<n;j++)  i.e. for(j=1;1a[j])  i.e. if(a[0]>a[1]) i.e. if(7>8)  false
2nd iteration for(j=i+1;j<n;j++)  i.e. for(j=2;2a[j])  i.e. if(a[0]>a[1]) i.e. if(7>12)  false

3rd iteration for(j=i+1;j<n;j++)  i.e. for(j=3;3a[j])  i.e. if(a[0]>a[1]) i.e. if(7>3)  true
temp=a[i];  i.e. temp=a[0]  i.e. temp=7
a[i]=a[j];  i.e. a[0]=a[3] i.e. a[0]=3
a[j]=temp; i.e. a[3]=7

So now are array is a={3,8,12,7}

2nd iteration for(i=1;i<n;i++) i.e. for(i=1;1<4;i++)   – Outer for loop
1st iteration for(j=i+1;j<n;j++)  i.e. for(j=2;2a[j])  i.e. if(a[1]>a[2]) i.e. if(8>12)  false

2nd iteration for(j=i+1;j<n;j++)  i.e. for(j=2;2a[j])  i.e. if(a[1]>a[3]) i.e. if(8>7) true
temp=a[i];  i.e. temp=a[1]  i.e. temp=8
a[i]=a[j];  i.e. a[1]=a[3] i.e. a[1]=7
a[j]=temp; i.e. a[3]=8

So now are array is a={3,7,12,8}

3rd iteration for(i=2;i<n;i++) i.e. for(i=2;2<4;i++)   – Outer for loop
1st iteration for(j=i+1;j<n;j++)  i.e. for(j=3;3a[j])  i.e. if(a[2]>a[3]) i.e. if(12>8)  true
temp=a[i];  i.e. temp=a[2]  i.e. temp=12
a[i]=a[j];  i.e. a[2]=a[3] i.e. a[2]=8
a[j]=temp; i.e. a[3]=12

So now are array is a={3,7,8,12}
Hence we have sorted the elements in ascending order a={3,7,8,12}.

Algorithm for Sort an Array Elements

  • START
  • INITIALIZE arr[] ={5, 2, 8, 7, 1 }.
  • SET temp =0
  • length= sizeof(arr)/sizeof(arr[0])
  • PRINT "Elements of Original Array"
  • SET i=0. REPEAT STEP 7 and STEP 8 UNTIL i<length
  • PRINT arr[i]
  • i=i+1.
  • SET i=0. REPEAT STEP 10 to STEP UNTIL i<n
  • SET j=i+1. REPEAT STEP 11 UNTIL jarr[j]) then
    temp = arr[i]
    arr[i]=arr[j]
    arr[j]=temp
  • j=j+1.
  • i=i+1.
  • PRINT new line
  • PRINT "Elements of the array sorted in ascending order"
  • SET i=0. REPEAT the below steps UNTIL i < length
  • PRINT arr[i]
  • i=i+1.
  • RETURN 0.
  • END.

C Program for Array Sorting

#include <stdio.h>    
     
int main()    
{      
    int arr[] = {50, 20, 80, 70, 10};     
    int temp = 0;    
           
    int length = sizeof(arr)/sizeof(arr[0]);    
         
    printf("Elements of original array: \n");    
    for (int i = 0; i < length; i++) {     
        printf("%d ", arr[i]);     
    }      
         
    for (int i = 0; i < length; i++) {     
        for (int j = i+1; j < length; j++) {     
           if(arr[i] > arr[j]) {    
               temp = arr[i];    
               arr[i] = arr[j];    
               arr[j] = temp;    
           }     
        }     
    }    
        
    printf("\n");    
            
    printf("Elements of array sorted in ascending order: \n");    
    for (int i = 0; i < length; i++) {     
        printf("%d ", arr[i]);    
    }    
    return 0;    
}     

Time Complexity of Ascending order in C: O(N2)
Auxiliary Space of Ascending order in C: O(1)

Tips for Ascending Order Program in C

A sorting algorithm is used to reorder an array or list of elements in accordance with an element comparison operator. The new order of the items in the relevant data structure is determined using the comparison operator. Therefore, This article is the basic of the sorting algorithm and if you got these basic sorting methods you will be able to solve more difficult problems of sorting algorithms as in data structures and algorithms sorting bits of help in every advanced topic.

Conclusion
In conclusion, sorting a set of numbers in ascending order is a fundamental operation in computer programming, and there are many different algorithms that can be used to accomplish this task. Whether you choose to use bubble sort, insertion sort, selection sort, or another sorting algorithm, the basic steps for writing a program to sort numbers in ascending order in C will remain the same.

Frequently Asked Questions

Q1. What is the purpose of sorting numbers in ascending order?
Ans. Sorting numbers in ascending order is a common task in programming and data analysis. It is useful when you want to organize data, identify patterns, or make calculations based on the order of the numbers.

Q2. Which sorting algorithm is the best for sorting numbers in ascending order?
Ans. There is no one-size-fits-all answer to this question, as the best algorithm will depend on the size of the data set, the distribution of the data, and other factors. Some commonly used sorting algorithms for ascending order include bubble sort, insertion sort, selection sort, and quicksort.

Q3. How do I declare an array in C for sorting numbers in ascending order?
Ans. You can declare an array of integers in C using the following syntax: int array_name[size_of_array]; where size_of_array is the number of elements in the array.

Q4. What is the time complexity of sorting algorithms for ascending order?
Ans.The time complexity of sorting algorithms for ascending order can vary depending on the algorithm used. For example, bubble sort has a worst-case time complexity of O(n^2), while quicksort has an average-case time complexity of O(n log n).

Q5. What is the difference between ascending order and descending order?
Ans. Ascending order refers to the arrangement of a set of numbers from smallest to largest, while descending order refers to the arrangement of a set of numbers from largest to smallest.


Other C Programs

C Program for Binary Search
C Program to Add Two Numbers
C Program to Calculate Percentage of 5 Subjects
C Program to Convert Binary Number to Decimal Number
C Program to Convert Celsius to Fahrenheit
C Program to Convert Infix to Postfix
C Program to Find Area of Circle
C Program to Find Roots of Quadratic Equation
C program to Reverse a Linked List
C program to reverse a number
Menu Driven Program For All Operations On Doubly Linked List in C
C Program for Armstrong Number
C Program For Merge Sort For Linked Lists
C program for performing Bubble sort on Linked List
Hello World Program in C
Perfect Number Program in C
Leap Year Program in C
Odd Even Program in C
Selection Sort Program in C

Leave a Reply

Your email address will not be published. Required fields are marked *