Sorting

Sorting means arranging the elements of the array in the some relevant order which may either be ascending or descending,arrange in sorting order.for example:If  A is an element,then the element of an array is arrange in ascending order in such a way that,A[0]<A[1]<A[2]<........A[n].
Sorting algorithm    Sorting algorithm is defined as an algorithm that elements of a list in certain order(that can be either numerical or any user defined order.Efficient sorting algorithms are widely used to optimize the use of other algorithms like search and merge algorithms.There are two types of sorting Internal sorting and external sorting.


Bubble Sort

Bubble sort is very simple method that sorts the array elements by repeatedly moving the largest element to the highest index position in the array.In bubble sorting,consecutive adjacent pairs of elements in the array are compared with each other.If the element at the lower index is greater than the element at the higher index,the two elements are interchanged so that the smaller element is placed before the bigger one.
 This procedure of sorting is called bubble sorting because the elements 'bubble' to the top of the list.At the end of the first pass, the largest element in the list will be placed at the end of the list.

Write a program to enter n numbers in an array.display the array with elements being sorted in ascending order.

#include<stdio.h>
#include<conio.h>

void main()

{

int i,temp,j,arr[10];

clrscr();

printf("\n Enter the number of elements in the array:")

scanf("%d",&n);

printf("Enter the elements");

for(i=0;i<n;i++)

scanf("%d",arr[i]);

for(i=0;i<n;i++)

{

for(j=0;j<n-i;j++)

{

if(arr[j]>arr[j+1])

{

temp=arr[j];

arr[j]=arr[j+1]);

arr[j+1]=temp;

}

}

}

printf("\n The array sorted in ascending order is: \n");

for(i=0;i<n;i++)

{

printf("\t %d",i,arr[i]);

getch();

}

}


Output

Enter the number of the element in array:6

Enter the elements 27 72 36 63 45 54

The array sorted in ascending order is:27 36 45 54 63 72


Comments