Monday, May 12, 2014

Java : SORTING

SORTING

1. Selection Sort : 

 1st  iteration -  find the least value (for ascending ) and swap it with the first element in the array.
2nd iteration - find the 2nd lowest starting (start searching from 2nd element) and swap it with 2nd element in the array.


import javax.swing.JOptionPane;

//SELECTION SORT IN ASCENDING ORDER
public class Sort {

    public static void main(String[] args) {
       
        int a[]={34,67,9,4,3,2,1};   //ARREY TO BE SORTED
        int i,j,small=a[0],temp;
        Boolean flag=false;
        String s="";
       
//        JOptionPane.showMessageDialog(null, a.length);
        for (i=0;i<=a.length-2;i++)
        {
            for(j=i;j<=a.length-(2+i);j++)
            {
                if(a[j+1]<a[j])
                {
                    small=a[j+1];
                    flag=true;
                }
                   
            }
           
            if(flag==true)
            {
                flag=false;
                temp=a[i];
                a[i]=small;
                a[j]=temp;                               
            }                       
        }
       
        for(i=0;i<=a.length-1;i++)
             s=s+a[i]+",";
       
        System.out.print(s);

    }

}


 2. Bubble Sort :

Works by swapping technique, where each pair of value is swapped depending on ascending r descending order required.  If there are 'n' elements , then there will n-1 iteartionsie., it there are 5 elements then there will be 4 iterations .If there are 5 elements , the 1st iteration will have 4 comparisons 

Ex : n=5

Iteration = 1 , Comparisons =4  ( At the end of 1st round the largest value is pushed to the bottom).

Iterations = 2 , Comparisons = 3

public class Bubble {

    public static void main(String[] args) {
       
        int a[]={4,3,2,1}; // ARRAY TO BE SORTED
        int temp;
        String s="";
               
       
        for(int i=0;i<a.length-1;i++)
        {
            for(int j=0;j<a.length-1;j++)
            {
                if(a[j]>a[j+1])
                {
                    temp=a[j];
                    a[j]=a[j+1];
                    a[j+1]=temp;
                   
                }
            }
        }
       
        for(int i=0;i<a.length;i++)
        {
            s=s+a[i]+",";
        }
        System.out.print(s);

    }

}

 3.Quick Sort (http://www.programcreek.com/2012/11/quicksort-array-in-java/) :

 
public class QuickSort {
 
 public static void main(String[] args) {
  int[] x = { 9, 2, 4, 7, 3, 7, 10 };
  printArray(x);
 
  int low = 0;
  int high = x.length - 1;
 
  quickSort(x, low, high);
  printArray(x);
 }
 
 public static void quickSort(int[] arr, int low, int high) {
 
  if (arr == null || arr.length == 0)
   return;
 
  if (low >= high)
   return;
 
  //pick the pivot
  int middle = low + (high - low) / 2;
  int pivot = arr[middle];
 
  //make left < pivot and right > pivot
  int i = low, j = high;
  while (i <= j) {
   while (arr[i] < pivot) {
    i++;
   }
 
   while (arr[j] > pivot) {
    j--;
   }
 
   if (i <= j) {
    int temp = arr[i];
    arr[i] = arr[j];
    arr[j] = temp;
    i++;
    j--;
   }
  }
 
  //recursively sort two sub parts
  if (low < j)
   quickSort(arr, low, j);
 
  if (high > i)
   quickSort(arr, i, high);
 }
 
 public static void printArray(int[] x) {
  for (int a : x)
   System.out.print(a + " ");
  System.out.println();
 }
}

 

No comments:

Post a Comment