Categories
Algorithm Algorithms & Design Sort

Quick Sort


public class QuickSort {

	public static void main(String[] args) {
		
		int arr[] = {1,12,9,14,16,80,33,3,4,7,60,2};
		
		System.out.println("printing an array");
		printArray(arr);
		
		quickSort(arr,0,arr.length-1);
		
		System.out.println("printing a sorted array");
		printArray(arr);
		
		
	}

	static void printArray(int arr[])
    {
        int n = arr.length;
        for (int i = 0; i < n; ++i)
            System.out.print(arr[i] + " ");
        System.out.println();
    }

	static void quickSort(int arr[], int lowIndex, int highIndex){
		
		if(lowIndex<highIndex){
			
		
			
			int midIndex = partition(arr,lowIndex,highIndex);
			System.out.println("partition array lowIndex::"+lowIndex+", highIndex::"+highIndex);
			printArray(arr);
			
			
			quickSort(arr,lowIndex,midIndex-1);
			quickSort(arr,midIndex+1,highIndex);
			
			
		
			
			
			
		}
		
	}
	
	static int partition(int arr[],int lowIndex, int highIndex){
		int pivot = arr[highIndex];
		
		int i = lowIndex-1;
		
		for(int j=lowIndex;j<=highIndex-1;j++){
			if(arr[j]<pivot){
				i++;
				swap(arr,i,j);
			}
		}

		swap(arr,i+1,highIndex);
		return i+1;
	}
	
	static void swap(int arr[], int i, int j){
		int temp = arr[i];
		arr[i]=arr[j];
		arr[j]=temp;
	}
}

Output :


printing an array
1 12 9 14 16 80 33 3 4 7 60 2 
partition array lowIndex::0, highIndex::11
1 2 9 14 16 80 33 3 4 7 60 12 
partition array lowIndex::2, highIndex::11
1 2 9 3 4 7 12 14 16 80 60 33 
partition array lowIndex::2, highIndex::5
1 2 3 4 7 9 12 14 16 80 60 33 
partition array lowIndex::2, highIndex::3
1 2 3 4 7 9 12 14 16 80 60 33 
partition array lowIndex::7, highIndex::11
1 2 3 4 7 9 12 14 16 33 60 80 
partition array lowIndex::7, highIndex::8
1 2 3 4 7 9 12 14 16 33 60 80 
partition array lowIndex::10, highIndex::11
1 2 3 4 7 9 12 14 16 33 60 80 
printing a sorted array
1 2 3 4 7 9 12 14 16 33 60 80 


Categories
Algorithm Algorithms & Design Sort

Merge Sort


public class MergeSort {

	public static void main(String[] args) {
		// TODO Auto-generated method stub

		 int arr[] = { 12, 11, 13, 5, 6, 7,99,87,0,2,103 };
		 
		 System.out.println("printing an array :::");
		 printArray(arr);
		 
		 sort(arr,0,arr.length-1);
		 
		 System.out.println("printing a sorted array :::");
		 printArray(arr);
	}

	
	static void printArray(int arr[])
    {
        int n = arr.length;
        for (int i = 0; i < n; ++i)
            System.out.print(arr[i] + " ");
        System.out.println();
    }
	
	public static void sort(int arr[], int startIndex, int endIndex){
		
		if(startIndex==endIndex)
			return;
		int midIndex = (startIndex+endIndex)/2;
		
		sort(arr,startIndex, midIndex);
		sort(arr,midIndex+1,endIndex);
		
		merge(arr, startIndex, midIndex, endIndex);
		
	}
	
	public static void merge(int arr[], int startIndex, int midIndex, int endIndex ){
		
		int size1 = midIndex-startIndex+1;
		int size2 = endIndex-midIndex;
		int firstPart[] = new int[size1];
		int secondPart[] = new int[size2];
		
		System.out.println("startIndex::"+startIndex+", midIndex::"+midIndex+", endIndex::"+endIndex);
		System.out.println("size1 ::"+size1);
		System.out.println("size2 ::"+size2);
		
		
		int i=0,j=0,k=0;
		
		//copy data into the array
		for(i=0;i<size1;i++){
			firstPart[i] = arr[startIndex+i];
		}

		for(j=0;j<size2;j++){
			secondPart[j]=arr[j+midIndex+1];
		}
		
		i=0;
		j=0;
		k=startIndex;
		
		System.out.println("firstPart::");
		printArray(firstPart);
		System.out.println("secondPart::");
		printArray(secondPart);
		
		
		while(i<size1 && j<size2){
			
			if(firstPart[i]<secondPart[j]){
				arr[k]=firstPart[i];
				i++;
				k++;
			} else if(firstPart[i]>=secondPart[j]){
				arr[k]=secondPart[j];
				j++;
				k++;
			}
			
		}
		
		while(i<size1){
			arr[k] = firstPart[i];
			i++;
			k++;
		}
		
		while(j<size2){
			arr[k]=secondPart[j];
			j++;
			k++;
		}
		
		System.out.println("Merget Array::");
		printArray(arr);
		
	}
}

Output :

printing an array :::
12 11 13 5 6 7 99 87 0 2 103 
startIndex::0, midIndex::0, endIndex::1
size1 ::1
size2 ::1
firstPart::
12 
secondPart::
11 
Merget Array::
11 12 13 5 6 7 99 87 0 2 103 
startIndex::0, midIndex::1, endIndex::2
size1 ::2
size2 ::1
firstPart::
11 12 
secondPart::
13 
Merget Array::
11 12 13 5 6 7 99 87 0 2 103 
startIndex::3, midIndex::3, endIndex::4
size1 ::1
size2 ::1
firstPart::
5 
secondPart::
6 
Merget Array::
11 12 13 5 6 7 99 87 0 2 103 
startIndex::3, midIndex::4, endIndex::5
size1 ::2
size2 ::1
firstPart::
5 6 
secondPart::
7 
Merget Array::
11 12 13 5 6 7 99 87 0 2 103 
startIndex::0, midIndex::2, endIndex::5
size1 ::3
size2 ::3
firstPart::
11 12 13 
secondPart::
5 6 7 
Merget Array::
5 6 7 11 12 13 99 87 0 2 103 
startIndex::6, midIndex::6, endIndex::7
size1 ::1
size2 ::1
firstPart::
99 
secondPart::
87 
Merget Array::
5 6 7 11 12 13 87 99 0 2 103 
startIndex::6, midIndex::7, endIndex::8
size1 ::2
size2 ::1
firstPart::
87 99 
secondPart::
0 
Merget Array::
5 6 7 11 12 13 0 87 99 2 103 
startIndex::9, midIndex::9, endIndex::10
size1 ::1
size2 ::1
firstPart::
2 
secondPart::
103 
Merget Array::
5 6 7 11 12 13 0 87 99 2 103 
startIndex::6, midIndex::8, endIndex::10
size1 ::3
size2 ::2
firstPart::
0 87 99 
secondPart::
2 103 
Merget Array::
5 6 7 11 12 13 0 2 87 99 103 
startIndex::0, midIndex::5, endIndex::10
size1 ::6
size2 ::5
firstPart::
5 6 7 11 12 13 
secondPart::
0 2 87 99 103 
Merget Array::
0 2 5 6 7 11 12 13 87 99 103 
printing a sorted array :::
0 2 5 6 7 11 12 13 87 99 103 

Categories
Algorithm Algorithms & Design Sort

Insertion Sort

public class InsertionSort {

	public static void main(String[] args) {
	
		int arr [] = {1,11,2,8,19,4,3,9}; 
		
		int temp;
		for(int i = 1; i < arr.length;i++){
			
			int j = i-1;
			while(j>=0 && arr[j]>arr[j+1]){
				temp = arr[j];
				arr[j]=arr[j+1];
				arr[j+1]=temp;
				j--;
			}
			
		}
		
		printArray(arr);

	}

	public static void printArray(int arr[]){
		for(int k=0;k<arr.length;k++)
			System.out.println(arr[k]);
	}
}

Output :

Categories
Algorithm Algorithms & Design Sort

Selection Sort


import java.util.ArrayList;

public class SelectionSort {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		int arr[] = {21,12,14,1,26,34,67,45,0};
		
		int minimum,temp;
		for(int i=0;i<arr.length;i++){
			minimum = i;
			for(int j=i+1;j<arr.length;j++){
				if(arr[j]<arr[minimum]){
					minimum = j;
				}
			}
			
			temp = arr[i];
			arr[i] = arr[minimum];
			arr[minimum]=temp;
		}
		
		System.out.println("Sorted array::");
		printArray(arr);
	}
	
	public static void printArray(int arr[]){
		for(int i=0;i<arr.length;i++)
			System.out.println(arr[i]);
	}
}

Output :


Sorted array::
0
1
12
14
21
26
34
45
67


Categories
Algorithm Algorithms & Design Sort

Bubble Sort


public class BubbleSort {

	public static void main(String[] args) {

		int arr[] = {21,12,14,1,26,34,67,45,0};
		int temp;
		for(int i=0;i<arr.length;i++){
			
			for(int j=0;j<(arr.length-i-1);j++){
				
				if(arr[j]>arr[j+1]){
					temp = arr[j];
					arr[j]=arr[j+1];
					arr[j+1]=temp;
				}
			}
		}
	
		printArray(arr);
		
	}

	public static void printArray(int array[]){
		
		System.out.println("Printing sorted array ::");
		for(int i=0;i<array.length;i++)
			System.out.println(array[i]);
		
	}
}

Output :


Printing sorted array ::
0
1
12
14
21
26
34
45
67

Categories
Algorithm Algorithms & Design Search

Exponential Search

public class ExponentialSearch {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		int arr [] = {2,4,5,6,7,8,9,10,11,12,13,14,15};
		
		int elementToSearch = 12;
		
		System.out.println("Searching for the element ::"+elementToSearch);
		int result = exponentialSearch(arr, arr.length,elementToSearch);
		System.out.println("Element found at index ::"+result);
		
	}

	public static int exponentialSearch(int arr[],int len, int elementToSearch){
		
		if(arr[0]==elementToSearch)
			return 0;
		
		int i =2;
		while(i<len && arr[i]<elementToSearch){
			i=i*2;
		}
		
		System.out.println("i ::"+i);
		int endIndex = (i>len)?len:i;
		
		BinarySearch binary = new BinarySearch();
		return binary.binarySearch(arr, i/2,endIndex, elementToSearch);
	}
}

Output :

Searching for the element ::12
i ::16
Element found at index ::9

Categories
Algorithm Algorithms & Design Search

Jump Search

public class JumpSearch {

	public static void main(String[] args) {
		// TODO Auto-generated method stub

		
		int arr[] = {0,1,21,32,44,56,61,76,83,94,101};
		
		int elementToSearch = 44;
		
		System.out.println("Searching for the element ::"+elementToSearch);
		
		int index = jumpSearch(arr,elementToSearch);
		
		
		System.out.println("Element found at index ::"+index);
	}

	public static int jumpSearch(int arr[], int elementToSearch){
		int index = -1;
		
		//finding the block size
		int blockSize = (int)Math.floor(Math.sqrt(arr.length));
		
		//finding the block where elementToSeach may be present;
		int blockStart = 0;
		while(arr[Math.min(blockSize, arr.length)-1] arr.length-1)
				return -1;
		}
		
		while(arr[blockStart] Math.min(blockSize, arr.length))
				return -1;
			
			if(arr[blockStart]==elementToSearch)
				return blockStart;
			
			
			blockStart++;
		}
		return index;
	}
	
}

Output :


Searching for the element ::44
Element found at index ::4

Categories
Algorithm Algorithms & Design Search

Fibonacci Search


public class FibonacciSearch {

	public static void main(String[] args) {
		int arr[]={10,22,33,34,36,37,39,41,44,48,51,57};
		
		int n = arr.length;
		int x = 44;
		
		System.out.println("Element to Search ::"+x);
		
		int index = fibonacci(arr,x,n);
		
		System.out.println("Element found at location ::"+index);
	}

	public static int fibonacci(int arr[],int x, int n){
		
		//find smallest fibonacci number greater than or equal to n;
		int fibM2 = 0;
		int fibM1 = 1;
		int fibM = fibM1+fibM2;
		
		while(fibM1){
			int i = Math.min(offset+fibM2, n-1);
			
			if(arr[i]x){
				fibM = fibM2;
				fibM1 = fibM1 - fibM2;
				fibM2 = fibM - fibM1;
			}
			
			else 
				return i;
		}
		
		
		   if (fibM1 == 1 && arr[n-1] == x)
	            return n-1;
		   
		   
		return -1;
	}
}


Output

Element to Search ::44
Element found at location ::8
Categories
Algorithm Algorithms & Design Search

Ternary Search


public class TernarySearch {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		
		int arr[] = {1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18};
		
		int elementToSearch = 2;
		System.out.println("Searching for the element ::"+elementToSearch);
		
		int resultIndex = tarnarySearch(arr,elementToSearch,0,arr.length-1);
		
		System.out.println("element found at index ::"+resultIndex);

	}

	public static int tarnarySearch(int arr[],int elementToSearch,int startIndex, int endIndex){
		
		int mid1 = startIndex+(endIndex-startIndex)/3;
		
		int mid2 = startIndex + ((endIndex-startIndex)/3)+((endIndex-startIndex)/3);
		
		if(arr[startIndex]==elementToSearch)
			return startIndex;
		else if(arr[endIndex]==elementToSearch)
			return endIndex;
		else if(arr[mid1]==elementToSearch)
			return mid1;
		else if(arr[mid1]>elementToSearch)
			return tarnarySearch(arr,elementToSearch,startIndex,mid1-1);
		else if(arr[mid2]==elementToSearch)
			return mid2;
		else if(arr[mid2]>elementToSearch)
			return tarnarySearch(arr,elementToSearch,mid1,mid2-1);
		else if(arr[endIndex]>=elementToSearch)
			return tarnarySearch(arr,elementToSearch,mid2,endIndex);
			
		
		return -1;
	}
}

Output :


Searching for the element ::2
element found at index ::1

Categories
Algorithm Algorithms & Design Search

Linear Search



public class LinearSearch {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		int arr[] = {1,2,3,4,5};
		int search_element=1;
		
		
		System.out.println("Element to be searched ::"+search_element);
		int index = search(arr,search_element);
		System.out.println("Element found at index ::"+index);
	}

	
	public static int search(int arr[],int search_element){
		int index=-1;
		int len = arr.length;
		int left = 0;
		int right = len-1;
		
		for(;left<=right;){
			
			if(arr[left]==search_element)
				return left;
			if(arr[right]==search_element)
				return right;
			
			left++;
			right--;
			
		}
		
		
		
		return index;
	}
}


Output :

Element to be searched ::1
Element found at index ::0
Design a site like this with WordPress.com
Get started