Sorting :data structure using java (part-1)

This will be a series of 4-5 for data structure including codes with demonstration . this is very first for sorting . follow for more and see more blogs on django ; mongodb : hackerrank solutions etc.

1. BUBBLE SORT :--

The most initial and basic sorting algorithm is bubble sort . We just compare left element of array to right element of array and swap it as per our need of ,as we want to arrange array in ascending or Descending order . 

package sorting;
import java.util.*;
import java.io.*;
public class Bubble_sort {
public static void main(String[] args) {
Scanner sn = new Scanner(System.in);
int array [] = new int [5];
for(int i=0 ; i<5;i++) {
array[i] =  sn.nextInt();
}
for(int i=0 ; i<5;i++) {
for(int j=i+1;j<5;j++) {
if(array[i]>array[j]) {
int temp = array[i];
array[i] = array[j];
array[j] = temp;
}
}
}
for(int k=0;k<5;k++) {
System.out.println(array[k]);
}
}
}




2 . INSERTION SORT :--

package sorting;
import java.util.*;
public class Insertion_Sort{
public static void main(String [] args) {
Scanner sn = new Scanner(System.in);
int a [] = {12,32,11,10,78};
for(int firstunsortedindex = 1; firstunsortedindex < a.length;firstunsortedindex++) {
     int newelement = a[firstunsortedindex] ;
     int i;
     for(i=firstunsortedindex;i>0 && a[i-1]>newelement ; i--) {
    a[i] = a[i-1];
     }
        a[i] = newelement;
}
for(int j=0;j<a.length;j++) {
System.out.println(a[j]);
}
}
}



3. QUICK SORT :--

package sorting;

public class Quick_sort {

public static void main(String[] args) {
// TODO Auto-generated method stub
int a [] = {12,32,11,10,78};
quicksort(a,0,a.length);
for(int i=0;i<a.length;i++) {
System.out.println(a[i]);
}


}
public static void quicksort(int input[], int start, int end) {
if(end - start <2) {
return;
}else {
int  pivotindex = partition(input, start, end);
quicksort(input, start, pivotindex);
quicksort(input, pivotindex+1, end);
}
}
public static int partition(int input[] ,int start, int end) {
int pivot  = input[start];
int i = start;
int j=end;
while(i<j) {
while(i<j && input[--j]>pivot);
if(i<j) {
input[i] = input[j];
}
while(i<j && input[++i]<pivot);
if(i<j) {
input[j] = input[i];
}
}
input[j]= pivot;
return j;
}

}

4. COUNTING SORT :--

package sorting;
import java.io.*;
import java.util.*;
import java.lang.*;
public class Counting_sort {

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

int a []= {2,3,4,12,1,2,3,12,7};
countsort(a,0,12);
for(int i=0;i<a.length;i++) {
System.out.println(a[i]);
}
}
//we have to make a function count sort
public static void countsort(int input[],int min,int max) {
int countarray [] = new int [(max-min) +1];
for(int i=0;i<input.length;i++) {
countarray[input[i]-min]++;
}
int j=0;
for(int i=min;i<=max;i++){
while(countarray[i-min]>0) {
input[j++] = i;
countarray[i-min]--;
}
}
}
}

5. SELECTION SORT :--

package sorting;
import java.io.*;
import java.util.*;
public class Selection_sort {

public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner sn  = new Scanner(System.in);
int a[] = new int[5];
int m = 0;
for(int i=0 ; i<5;i++) {
a[i] =  sn.nextInt();
}
int max = 0;
for(int i=4;i>0;i--) {
int largest =0;
for(int j=1;j<i+1;j++) {
if(a[j] > a[largest]) {
largest = j;
}
}

int temp = a[i];
a[i] = a[largest];
a[largest] = temp;
}
for(int k=0;k<5;k++) {
System.out.println(a[k]);
}
}
}


-- hope that this will help the freshers a lot to get start with sorting .
follow for more .
@mazerunner


Comments

Popular posts from this blog

Amazon Web Services

Hacker Rank all java and python problem solutions

Google Code-In mentorship experience :)