-
Notifications
You must be signed in to change notification settings - Fork 62
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #3 from ADVAITH18/master
Sorting Algorithms
- Loading branch information
Showing
3 changed files
with
99 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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]+" "); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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]); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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]+" "); | ||
} | ||
} |