Data Structures programs in Java

In this post we will look at very important subject in computer science, every computer science student must know data structure and how to implement it. Not only in theory but he/she must know how to code it. Sounds boring !!!. Forget whatever I said above, just code for fun. There's no rule to follow in tech.
Here's the code:

Bubble Sort in java:

from the Wikipedia article:

Bubble sort, sometimes referred to as sinking sort, is a simple sorting algorithm that repeatedly steps through the list to be sorted, compares each pair of adjacent items and swaps them if they are in the wrong order. The pass through the list is repeated until no swaps are needed, which indicates that the list is sorted. The algorithm, which is a comparison sort, is named for the way smaller elements "bubble" to the top of the list. Although the algorithm is simple, it is too slow and impractical for most problems even when compared to insertion sort.[1] It can be practical if the input is usually in sorted order but may occasionally have some out-of-order elements nearly in position.



public class BubbleSort 

{
 public static int counter;
 public static void main(String args[]
 {
  int[] arr={9,8,7,6,5,4,3,2,1};
  for(int i=0;i<arr.length;count++)
  {
   for(int j=1;j<arr.length;j++)
   {
    if(arr[j-1]>arr[j])
    {
     int temp;
     temp=arr[j-1];
     arr[j-1]=arr[j];
     arr[j]=temp;
     System.out.println();
     for(int k=0;k<arr.length;k++)
     {
      System.out.print(arr[k]+" ");
     }
    }
   }
  }
 }
}



Insertion Sort in java


In this sorting algorithm elements are shifted to right as per the order(here we used ascending order) and the element which is selected for comparison with others is shifted the left side, as we do in while playing cards, we place the card in correct order in left hand(sorted cards).



public class InsertionSort
{
public static void main(String args[])
{
int a[] = {5,2,4,3,1};
int key;
int i;
for(int j = 1; j < a.length; j++)
{
key = a[j];
i = j - 1;
while(i>=0 && a[i]>key)
{
a[i+1]= a[i];
i--;
}
a[i+1] = key;
for(int k=0; k<a.length;k++)
{
System.out.print(a[k]+" ");
}
System.out.println();
}
}
}

Selection Sort in java

From wikipedia article:

The algorithm divides the input list into two parts: the sublist of items already sorted, which is built up from left to right at the front (left) of the list, and the sublist of items remaining to be sorted that occupy the rest of the list. Initially, the sorted sublist is empty and the unsorted sublist is the entire input list. The algorithm proceeds by finding the smallest (or largest, depending on sorting order) element in the unsorted sublist, exchanging (swapping) it with the leftmost unsorted element (putting it in sorted order), and moving the sublist boundaries one element to the right.

public class SelectionSort 
{
 public static void main(String args[])
 {
  int[] arr={1,3,2,4,6,5,8,7,9};
  int current_min=0;
  int position_of_min=0;

  for(int i=0;i<arr.length;i++)
  {
   current_min=arr[i];
   for(int j=i+1;j<arr.length;j++)
   {
    //check if the current min number is greater than next no.
    if(current_min>arr[j])
    {
     current_min=arr[j];
     position_of_min=j;

     //this will perform a swap
     arr[i]=arr[i]+arr[position_of_min];
     arr[position_of_min]=arr[i]-arr[position_of_min];
     arr[i]=arr[i]-arr[position_of_min];
    }
   }
  }

  for(int k=0;k<arr.length;k++)
  {
   System.out.print(arr[k]+" ");
  }
 }
}

I will keep editing this post to add more programs in Data structure.


Comments

Popular posts from this blog

Integer.MAX_VALUE, Double.POSITIVE_INFINITY and Double.NaN in Java