Showing posts with label Sorting. Show all posts
Showing posts with label Sorting. Show all posts

Friday, 15 July 2016

Counting Sort

Following code show counting sort implementation in Java .

Counting Sort :   

class CountingSort
{
  public static void main(String[] args) {
    char arr[] = {'g', 'e', 'e', 'k', 's', 'f', 'o', 'r', 'g', 'e', 'e', 'k', 's'};
    sort(arr);
    System.out.print("Sorted character array is ");
        for (int i=0; i<arr.length; ++i)
            System.out.print(arr[i]);
  }
  static void sort(char arr[])
  {
    int n = arr.length;
    int count[] = new int[256];
    for (int i=0; i<256; ++i)
        count[i] = 0;
    for (int i=0; i<n; ++i)
        ++count[arr[i]];
    for (int i = 0,j=0; i<n;j++)
      {  while(count[j]>0)
         {
           arr[i] = (char)j;
           --count[j];
           i++;
         }
      }
  }

}
 

Wednesday, 13 July 2016

Selection Sort

Selection sort is a simple sorting algorithm.Smallest element is selected from the unsorted array and swapped with the leftmost element and that element becomes part of sorted array. This process continues moving unsorted array boundary by one element to the right.

 This algorithm is not suitable for large data sets as its average and worst case complexity are of O(n2) where n are no. of items.

Following  program shows selection sort algorithm in JAVA.

Selection Sort : 
public class SelectionSort {

    public static void main(String args[]) {
        int array[] = { 5, 2, 7, 4, 9, 0 };
        array = selectionSort(array);
        for (int i = 0; i < array.length; i++) {
            System.out.print(array[i]);
        }
    }

    private static int[] selectionSort(int array[]) {
        for (int i = 0; i < array.length; i++) {
            for (int j = i; j < array.length; j++) {
                if (array[i] > array[j]) {
                    int temp;
                    temp = array[j];
                    array[j] = array[i];
                    array[i] = temp;

                }
            }
        }
        return array;
    }

}

Tuesday, 12 July 2016

BubbleSort

This Java bubble sort example shows how to sort an array of int using bubble sort algorithm. Bubble sort is the simplest sorting algorithm.

BubbleSort  :

package sorting;

public class BubbleSort {

    public static void main(String args[]) {
        int array[] = { 5, 2, 7, 4, 9, 0 };
        array = bubbleSort(array);
        for (int i = 0; i < array.length; i++) {
            System.out.print(array[i]);
        }
    }

    private static int[] bubbleSort(int array[]) {
        for (int i = 0; i < array.length; i++) {
            for (int j = 0; j < array.length - i - 1; j++) {
                if (array[j] > array[j + 1]) {
                    int temp;
                    temp = array[j];
                    array[j] = array[j + 1];
                    array[j + 1] = temp;

                }
            }
        }
        return array;
    }

}
 

QuickSort

Following program show Quicksort Algorithm in Java.

QuickSort:

public class QuickSort {
    public static void main(String args[]) {
        int array[]={9,2,5,1,7,1,2,9,5,4};
        quickSort(array,0,array.length-1);
        for(int e : array)
        {
            System.out.println(""+e);
        }
    }

    static void quickSort(int [] arr , int left ,int right)
    {
        if(left<right)
        {
        int p = partition(arr,left,right);
        quickSort(arr,left,p-1);
        quickSort(arr,p+1,right);
        }
    }

static int partition(int [] arr,int l, int h)
{
    int pivot=arr[h];
    int i=l-1;
    for(int j=l;j<h;j++)
    {
        if(arr[j]<=pivot)
        {
            swap(arr,i+1,j);
            i++;
        }
    }
swap(arr,i+1,h);
return i+1;
}
static void swap(int arr[] ,int i , int j)
{
    int temp= arr[i];
    arr[i]=arr[j];
    arr[j]=temp;
}

}