JAVA SORTING PROGRAM

Sorting is technique which is used to show the elements in the ascending or descending order.

Types of sorting:

1.Internal sorting

2.External sorting

1.Internal sorting

If the number of elements/objects is small then we need to keep the elements/objects in the main memory,sorting is called internal sorting.

2.External sorting

When the number of elements is so large then we need to keep the elements/objects in the external memory,sorting is called external sorting.

Here we will discuss some internal sorting algorithms:

1.Bubble sort

2.Selection sort

3.Insertion sort

1.Bubble sort

It is a comparison-based algorithms in which current elements are compared with the next element and then elements are swapped if they  are not proper order.

It is good for the small amount of data or elements.

The time complexity of bubble sort is O(n^2).

It sorts the elements in (n-1) iteration.

Example:

4 3 2 9 7 1

Here there are 6 element so iteration will be (6-1)=5.

First Iteration:

4 3 2 9 7 1

3 4 2 9 7 1

3 2 4 9 7 1

3 2 4 7 9 1

3 2 4 7 1 9

and continue to the next Iteration in the same manner.

Bubble sort program:

public class BubbleSortProgram
{
static void bubbleSort(int []arr)
{
int n=arr.length;
int temp=0;
for(int i=0;i<n;i++)
{
for(int j=0;i<n-i;j++)
{
if(arr[j-1]>arr[j])
{
//Here we are swapping the element
temp=arr[j-1];
arr[j-1]=arr[j];
arr[j]=temp;
}
}
}
}
public static void main(String[] arg)
{
int arr[]={4,1,8,2,0,7};
System.out.println("Before the sorting");
for(int i=0;i<arr.length;i++)
{
System.out.print(arr[i]+" ");
}
System.out.println();
bubbleSort(arr);//Here elements are in the sorted form

System.out.prinln("After the sorting");
for(int i=0;i<arr.length;i++)
{
System.out.print(arr[i]+ " ");
}
}
}

Output:

Before the sorting
4 1 8 2 0 7
After the sorting
0 1 2 4 7 8

2 .Selection sort

In the selection sort algorithms,we search the lowest element in the array and and put the element into the proper location.that means current element swapped with the next lowest element in the array.

The time complexity of selection sort is O(n^2).

Example:

4 2 6 1 9 3 //Here 1 is smallest element so put it on the first index and 4 will be on the place of 1.

1 2 6 4 9 3 //Here 2 is in the proper place so there is no need to exchange or swap.

1 2 3 4 9 6 //Here 3 is not in the proper place so swapped with the 6.

1 2 3 4 9 6 //Here 4 is in the proper place so there is no need to swap.

1 2 3 4 6 9 //6 is the not proper place so swapped with the 9.

So the above example we can see the it takes (n-1) iteration to sort the elements.

Selection sort program:

public class SelectionSortProgram
{
public static void selection(int [] arr)
{
for(int i=0;i<arr.length-1;i++)
{
int index=i;
for(int k=i+1;i<arr.length;j++)
{
if(arr[k]<arr[index]
{
index=k;//Here we are searching the lowest index
}
}
int number=arr[index];
arr[index]=arr[i];
arr[i]=number;
}
}
public static void main(String[] arg)
{
int arr[]={4,2,6,1,9,3};
System.out.println("Before sorting the element");
for(int i=0;i<arr.length;i++)
{
System.out.print(arr[i]+" ");
}
System.out.println();
selection(arr);//Here sorting the element

System.out.println("After sorting the element");
for(int i=0;i<arr.length;i++)
{
System.out.print(arr[i]+" ");
}
}
}

Output

Before sorting the element
4 2 6 1 9 3
After sorting the element
1 2 3 4 6 9

3.Insertion sort

Insertion sort is the simple sorting algorithms as compare to other algorithms.

In the insertion sort,we compare the current element with the adjacent element and put the proper place.

The time complexity of insertion sort is O(n^2)

Example:

4 1 5 3 8 2

1 4 5 3 8 2//compare 1 and 4 then swapped them

1 4 5 3 8 2//compare 4 and 5.It is proper place so there is no need to swap.

1 3 4 5 8 2//putting the 3 to the proper place

1 3 4 5 8 2 //comparing 5 and 8 ,it is proper place so there is no need to swap

1 2 3 4 5 8 //putting the 2 to the proper place.

Insertion sort program

public class InsertionSortProgram
{
public static void insertion(int []arr)
{
  for(int k=1; k<10; k++)   
    {  
        int temp = a[k];  
        int j= k-1;  
        while(j>=0 && temp <= a[j])  
        {  
            a[j+1] = a[j];   
            j = j-1;  
        }  
        a[j+1] = temp;  
    }   
}
public static void main(String[] arg)
{
int arr[]={4,1,5,3,8,2};
System.out.println("Before sorting the element");
for(int k=0;k<arr.length;k++)
{
System.out.print(arr[k]+" ");
}
insertion(arr);
System.out.println("After sorting the element");
for(int k=0;k<arr.length;k++)
{
System.out.print(arr[k]+" ");
}
}
}

Output:

Before sorting the element
4 1 5 3 8 2
After sorting the element
1 2 3 4 5 8

sumankumar80

Hello Friends,I have written and developed this website because you can learn technology easily ,prepare for the placement and also for the any type of examination . By :Suman Kumar

Leave a Reply

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