Skip to content

Commit 0fbce19

Browse files
some sorting algorithms are added in java language
1 parent 8dcd792 commit 0fbce19

File tree

7 files changed

+149
-0
lines changed

7 files changed

+149
-0
lines changed

.DS_Store

6 KB
Binary file not shown.

Algorithms/.DS_Store

6 KB
Binary file not shown.

Algorithms/java/.DS_Store

6 KB
Binary file not shown.
+34
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
import java.util.Arrays;
2+
import java.util.Scanner;
3+
4+
public class BubbleSort { //For Ascending order.
5+
public static void main(String[] args) {
6+
Scanner sc = new Scanner(System.in);
7+
int n;
8+
n=sc.nextInt();
9+
int[] arr = new int[n];
10+
for(int i=0;i<n;i++)
11+
{
12+
arr[i]=sc.nextInt();
13+
}
14+
bubblesort(arr);
15+
System.out.println(Arrays.toString(arr));
16+
}
17+
18+
public static void bubblesort(int[] arr) {
19+
for(int i=0;i<arr.length;i++)
20+
{
21+
for(int j=1;j<arr.length-i;j++)
22+
{
23+
if(arr[j-1]>arr[j])
24+
{
25+
int temp; //swapping
26+
temp=arr[j];
27+
arr[j]=arr[j-1];
28+
arr[j-1]=temp;
29+
}
30+
}
31+
}
32+
}
33+
34+
}
+35
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
import java.util.Arrays;
2+
import java.util.Scanner;
3+
4+
public class CyclicSort { /**cyclic sort varies with the given range of numbers in the
5+
array this algorithm is for [0 , N-1] and the size of array is 'N' **/
6+
public static void main(String[] args) {
7+
Scanner sc = new Scanner(System.in);
8+
int n,k=0,temp;
9+
n= sc.nextInt();
10+
int[] arr = new int[n];
11+
for(int i=0;i<n;i++)
12+
{
13+
arr[i]=sc.nextInt();
14+
}
15+
cyclicsort(arr,n);
16+
System.out.println(Arrays.toString(arr));
17+
}
18+
19+
public static void cyclicsort(int[] arr, int n) {
20+
int k=0,temp=0;
21+
while(k<n)
22+
{
23+
if(arr[k] == k)
24+
{
25+
k++;
26+
}
27+
else
28+
{
29+
temp=arr[k];
30+
arr[k]=arr[temp];
31+
arr[temp]=temp;
32+
}
33+
}
34+
}
35+
}
+38
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
import java.util.Arrays;
2+
import java.util.Scanner;
3+
4+
public class InsertionSort {
5+
public static void main(String[] args) {
6+
Scanner sc = new Scanner(System.in);
7+
int n;
8+
n= sc.nextInt();
9+
int[] arr = new int[n];
10+
for(int i=0;i<n;i++)
11+
{
12+
arr[i]=sc.nextInt();
13+
}
14+
insertionsort(arr,n);
15+
System.out.println(Arrays.toString(arr));
16+
17+
}
18+
19+
public static void insertionsort(int[] arr, int n) {
20+
for(int i=0;i<n-1;i++)
21+
{
22+
for(int j=1+i;j>0;j--)
23+
{
24+
if(arr[j]<arr[j-1])
25+
{
26+
int temp; //swapping
27+
temp=arr[j];
28+
arr[j]=arr[j-1];
29+
arr[j-1]=temp;
30+
}
31+
else
32+
{
33+
break;
34+
}
35+
}
36+
}
37+
}
38+
}
+42
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
import java.util.Arrays;
2+
import java.util.Scanner;
3+
4+
public class SelectionSort {
5+
public static void main(String[] args) {
6+
Scanner sc = new Scanner(System.in);
7+
int n,temp1,temp2;
8+
n=sc.nextInt();
9+
temp1=n;
10+
temp2=n;
11+
int[] arr = new int[n];
12+
for(int i=0;i<n;i++)
13+
{
14+
arr[i]=sc.nextInt();
15+
}
16+
for(int i=0;i<n;i++)
17+
{
18+
swap(arr,findHighInd(arr,--temp1),--temp2);
19+
}
20+
System.out.println(Arrays.toString(arr));
21+
}
22+
23+
public static int findHighInd(int[] arr, int num) {
24+
int temp=arr[0],real=0;
25+
for(int i=0;i<=num;i++)
26+
{
27+
if(temp<arr[i])
28+
{
29+
temp=arr[i];
30+
real=i;
31+
}
32+
}
33+
return real;
34+
}
35+
public static void swap(int[] arr, int var1,int var2) {
36+
int temp;
37+
temp=arr[var1];
38+
arr[var1]=arr[var2];
39+
arr[var2]=temp;
40+
}
41+
42+
}

0 commit comments

Comments
 (0)