Sorting arrays in C

Sorting an array isn’t an easy task for beginners especially when they are new to procedure oriented languages like C, with the help of loops and conditional statements, we can sort the arrays easily.

Sorting an array isn’t an easy task for beginners especially when they are new to procedure oriented languages like C, with the help of loops and conditional statements, we can sort the arrays easily, today I’ll explain how to sort an array [numbers only] in C.

I’ve created two versions of this code :

  1. for beginners;
  2. Advanced;

The simple one

#include<stdio.h>
int sort(int *,int );    //function to sort the array
int main() {
  int length,i;
  printf("Enter the size of array : ");    //size of array
  scanf("%d",&length);
  int arr[length];
  printf("Enter the elements now :\n");
  for (i = 0; i < length; i++) {
    scanf("%d",&arr[i] );
  }
  printf("Array before sorting :\t");
  for (i = 0; i < length; i++) {
    printf("%d\t",arr[i] );
  }
  sort(&arr[0],length);        // sorting the array
  printf("\nArray after sorting :\t");
  for (i = 0; i < length; i++) {
    printf("%d\t",arr[i] );
  }
}

int sort(int *arr,int length) {
  int i,j,temp;
  for(i=0;i<length;i++) {
    for(j=i;j<length;j++) {
      if (arr[i]>arr[j]) {
        temp=arr[i];
        arr[i]=arr[j];
        arr[j]=temp;
      }
    }
  }
}
//sorting.c with ❤ by krypton 
Simple sorting

Here is a slightly advanced code


#include<stdio.h>
#define des    //write des for DESCENDING defaul : ASCENDING
int sort(int *,int );  //function to sort the array
int ArrayIO(int *,int ,int );  //to scan and/or printthe elements in the array
int main(){
  printf("Enter the size of array : ");    //size of array
  int length;
  scanf("%d",&length);
  int arr[length];
  printf("Enter the elements now : \n");
  ArrayIO(arr,length,1);      // 1 is for scanf
  printf("Org. array :\t");
  ArrayIO(arr,length,2);      // 2 is for printf
  sort(arr,length);           // sorting the array
  #ifdef des
    printf("ARRAY DESC :\t");
  #endif
  #ifndef des
    printf("ARRAY ASC :\t");
  #endif
  ArrayIO(arr,length,2);       // printing sorted array
}

int sort(int *arr,int length){
  for(int i=0;i<length;i++)
    {
      for(int j=i;j<length;j++)
        {
        #ifdef des
		if (arr[i]<arr[j]){     // swapping the elements in DESCENDING order
          arr[i]=arr[j]+arr[i]; // without using another temporary variable
          arr[j]=arr[i]-arr[j];
          arr[i]=arr[i]-arr[j];
        }
		#endif
        #ifndef des
          if (arr[i]>arr[j]){     // swapping the elements in ASCENDING order
            arr[i]=arr[j]+arr[i]; // without using another temporary variable
            arr[j]=arr[i]-arr[j];
            arr[i]=arr[i]-arr[j];
          }
        #endif
        }
    }
}


int ArrayIO(int *arr,int length,int operation){
  for(int i=0;i<length;i++){
    if(operation==1){
      scanf("%d",&arr[i]);
    } else {
        printf("%d\t",arr[i]);
      }
    }
  printf("\n");
}
//sorting.c with ❤ by krypton 
Advanced sorting

Thanks for visiting.

#conclusion

sorting arrays is a tough task for beginners, especially for the procedure oriented languages like C, but practise is the key, you can sort the arrays in C simply with the help of for-loops as well as by performing some simple but confusing operations on the variables to eliminate the need of the extra variables.