Skip to content

Commit

Permalink
Merge pull request #3 from ADVAITH18/master
Browse files Browse the repository at this point in the history
Sorting Algorithms
  • Loading branch information
darpanjbora authored Oct 13, 2019
2 parents b35aaff + bb49ab2 commit 59b9c09
Show file tree
Hide file tree
Showing 3 changed files with 99 additions and 0 deletions.
30 changes: 30 additions & 0 deletions Sorting Algorithms/bubble_sort.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import java.io.*;
class Bubble
{
public static void main()throws IOException
{
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
int i,j,t,n;
System.out.println("Enter the size of the array");
n=Integer.parseInt(br.readLine());
int a[]=new int[n];
System.out.println("Enter the array elements");
for(i=0;i<n;i++)
a[i]=Integer.parseInt(br.readLine());
System.out.println("ORIGINAL ARRAY");
for(i=0;i<n;i++)
System.out.print(a[i]+" ");
System.out.println();
for(i=0;i<n;i++)
for(j=0;j<n-1;j++)
if(a[j]>a[j+1])
{
t=a[j];
a[j]=a[j+1];
a[j+1]=t;
}
System.out.println("SORTED ARRAY");
for(i=0;i<n;i++)
System.out.print(a[i]+" ");
}
}
39 changes: 39 additions & 0 deletions Sorting Algorithms/insertion_sort.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
import java.io.*;
class Insertion
{
public static void main()throws IOException
{
BufferedReader br=new BufferedReader (new InputStreamReader(System.in));
int n,l,key,i,j;
System.out.println("Enter the size of the array");
n=Integer.parseInt(br.readLine());
int a[]=new int[n];
System.out.println("Enter the array elements");
for(i=0;i<n;i++)
a[i]=Integer.parseInt(br.readLine());
System.out.println("ORGINAL ARRAY : ");
for(i=0;i<n;i++)
System.out.println(a[i]);

//Sorting algorithm
for(i=1;i<n;i++)
{
key = a[i];
j = i - 1;
/* Move elements of arr[0..i-1], that are
greater than key, to one position ahead
of their current position */
while (j >= 0 && a[j] > key)
{
a[j + 1] = a[j];
j = j - 1;
}
a[j+1]=key;

}
//Displaying the array
System.out.println("SORTED ARRAY : ");
for(i=0;i<n;i++)
System.out.println(a[i]);
}
}
30 changes: 30 additions & 0 deletions Sorting Algorithms/selection_sort.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import java.io.*;
class Selection
{
public static void main()throws IOException
{
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
int i,j,t,n;
System.out.println("Enter the size of the array");
n=Integer.parseInt(br.readLine());
int a[]=new int[n];
System.out.println("Enter the array elements");
for(i=0;i<n;i++)
a[i]=Integer.parseInt(br.readLine());
System.out.println("ORIGINAL ARRAY");
for(i=0;i<n;i++)
System.out.print(a[i]+" ");
System.out.println();
for(i=0;i<n;i++)
for(j=i+1;j<n;j++)
if(a[i]>a[j])
{
t=a[i];
a[i]=a[j];
a[j]=t;
}
System.out.println("SORTED ARRAY");
for(i=0;i<n;i++)
System.out.print(a[i]+" ");
}
}

0 comments on commit 59b9c09

Please sign in to comment.