Skip to content

Commit 59b9c09

Browse files
authored
Merge pull request darpanjbora#3 from ADVAITH18/master
Sorting Algorithms
2 parents b35aaff + bb49ab2 commit 59b9c09

File tree

3 files changed

+99
-0
lines changed

3 files changed

+99
-0
lines changed

Sorting Algorithms/bubble_sort.java

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
import java.io.*;
2+
class Bubble
3+
{
4+
public static void main()throws IOException
5+
{
6+
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
7+
int i,j,t,n;
8+
System.out.println("Enter the size of the array");
9+
n=Integer.parseInt(br.readLine());
10+
int a[]=new int[n];
11+
System.out.println("Enter the array elements");
12+
for(i=0;i<n;i++)
13+
a[i]=Integer.parseInt(br.readLine());
14+
System.out.println("ORIGINAL ARRAY");
15+
for(i=0;i<n;i++)
16+
System.out.print(a[i]+" ");
17+
System.out.println();
18+
for(i=0;i<n;i++)
19+
for(j=0;j<n-1;j++)
20+
if(a[j]>a[j+1])
21+
{
22+
t=a[j];
23+
a[j]=a[j+1];
24+
a[j+1]=t;
25+
}
26+
System.out.println("SORTED ARRAY");
27+
for(i=0;i<n;i++)
28+
System.out.print(a[i]+" ");
29+
}
30+
}
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
import java.io.*;
2+
class Insertion
3+
{
4+
public static void main()throws IOException
5+
{
6+
BufferedReader br=new BufferedReader (new InputStreamReader(System.in));
7+
int n,l,key,i,j;
8+
System.out.println("Enter the size of the array");
9+
n=Integer.parseInt(br.readLine());
10+
int a[]=new int[n];
11+
System.out.println("Enter the array elements");
12+
for(i=0;i<n;i++)
13+
a[i]=Integer.parseInt(br.readLine());
14+
System.out.println("ORGINAL ARRAY : ");
15+
for(i=0;i<n;i++)
16+
System.out.println(a[i]);
17+
18+
//Sorting algorithm
19+
for(i=1;i<n;i++)
20+
{
21+
key = a[i];
22+
j = i - 1;
23+
/* Move elements of arr[0..i-1], that are
24+
greater than key, to one position ahead
25+
of their current position */
26+
while (j >= 0 && a[j] > key)
27+
{
28+
a[j + 1] = a[j];
29+
j = j - 1;
30+
}
31+
a[j+1]=key;
32+
33+
}
34+
//Displaying the array
35+
System.out.println("SORTED ARRAY : ");
36+
for(i=0;i<n;i++)
37+
System.out.println(a[i]);
38+
}
39+
}
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
import java.io.*;
2+
class Selection
3+
{
4+
public static void main()throws IOException
5+
{
6+
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
7+
int i,j,t,n;
8+
System.out.println("Enter the size of the array");
9+
n=Integer.parseInt(br.readLine());
10+
int a[]=new int[n];
11+
System.out.println("Enter the array elements");
12+
for(i=0;i<n;i++)
13+
a[i]=Integer.parseInt(br.readLine());
14+
System.out.println("ORIGINAL ARRAY");
15+
for(i=0;i<n;i++)
16+
System.out.print(a[i]+" ");
17+
System.out.println();
18+
for(i=0;i<n;i++)
19+
for(j=i+1;j<n;j++)
20+
if(a[i]>a[j])
21+
{
22+
t=a[i];
23+
a[i]=a[j];
24+
a[j]=t;
25+
}
26+
System.out.println("SORTED ARRAY");
27+
for(i=0;i<n;i++)
28+
System.out.print(a[i]+" ");
29+
}
30+
}

0 commit comments

Comments
 (0)