Sorting is the algorithm that can put the elements of a list in a certain order. In fact, the Sorting is used to specify the most used orders such as numerical order and lexicographical order. Rather, this algorithm is important for optimizing the use of other algorithms which includes input data that are to be sorted list. Most often, it is used for canonicalizing the data and for producing human readable output. Most probably, it can be used to satisfy two conditions such as,

- The output is in nondecreasing order
- The output is a permutation of the input

In addition, the data are taken in the array order and allows for the random access for sorting the list in sequential access.

Sorting is performed on the basis of a particular data items called as key. depending on the type of the key, records can be sorted either numerically or alphabetically.

There are several sorting techniques, that are generally used

2. Bubble Sort

4. Quick Sort

5. Radix Sort

6.Merge Sort

7. Heap Sort

Bubble Sorting:

This is an alternate way of putting the largest element at the highest index in the array uses an algorithm called bubble sort. This is the most straightforward and be a selection sort popular used to illustrate the sorting. Bubble sort is the most often used sorting technique. this method compares the two consecutive elements of the list and if they are not in order the two elements will be interchanged immediately. if they are in order the two elements will not be interchanged. this process continues for (N-1) times for sorting an array of size N.

Algorithm :

Input : a[] is the array of elements

output: a[0] <= a[1] <= ——– <= a[n-1]

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

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

if(a[i] > a[j+1])

swap (a[j],a[j+1])

end if

end for

end for

after the first phase the largest element will be in the Nth position. in the seconf phase the secont largest element in the N-1 th position and so on.. thus in each pass an element an element with largest value in the remaining unsorted element will be “bubble up” and hence the name is bubble sort.

import java.util.Scanner; /** * * @author chandrashekhar */ public class BubbleSortDemo { int a[]; int n; public BubbleSortDemo(int size) { n = size; a = new int[n]; } public void readArray() { Scanner s = new Scanner(System.in); System.out.println("Enter " + n + " Elements"); for (int i = 0; i < n; i++) { a[i] = s.nextInt(); } } public void showArray() { System.out.println(""); for (int i = 0; i < n; i++) { System.out.print(a[i] + " "); } System.out.println(""); } public void bubbleSort() { for (int i = 0; i < n - 1; i++) { for (int j = 0; j < n - i - 1; j++) { if (a[j] > a[j + 1]) { int temp = a[j]; a[j] = a[j + 1]; a[j + 1] = temp; } } System.out.println("After " + (i + 1) + " Pass"); showArray(); } } public static void main(String[] args) { Scanner s = new Scanner(System.in); System.out.println("Enter Array Size : "); int n = s.nextInt(); BubbleSortDemo bubbleSortDemo = new BubbleSortDemo(n); bubbleSortDemo.readArray(); System.out.println("Before Bubble Sort"); bubbleSortDemo.showArray(); bubbleSortDemo.bubbleSort(); System.out.println("After Bubble Sort"); bubbleSortDemo.showArray(); } } Output: Enter Array Size : 5 Enter 5 Elements : 9 5 1 6 0 Before Bubble Sort : 9 5 1 6 0 After 1 Pass : 5 1 6 0 9 After 2 Pass : 1 5 0 6 9 After 3 Pass : 1 0 5 6 9 After 4 Pass : 0 1 5 6 9 After Bubble Sort : 0 1 5 6 9