Skip to content

Commit ae5352f

Browse files
SpreehaMadhavBahl
authored andcommitted
day 32 (#224)
1 parent 9f5c3f6 commit ae5352f

File tree

2 files changed

+76
-0
lines changed

2 files changed

+76
-0
lines changed

day32/Java/selectionSort.java

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
/*
2+
* To change this license header, choose License Headers in Project Properties.
3+
* To change this template file, choose Tools | Templates
4+
* and open the template in the editor.
5+
*/
6+
package javaapplication3;
7+
8+
/**
9+
* @date 01/02/19
10+
* @author SPREEHA DUTTA
11+
*/
12+
import java.util.*;
13+
public class selectionSort {
14+
public static void main(String []args)
15+
{
16+
int n,i,j,t,min=0;
17+
Scanner sc=new Scanner(System.in);
18+
n=sc.nextInt();
19+
int arr[]=new int[n];
20+
for(i=0;i<n;i++)
21+
arr[i]=sc.nextInt();
22+
for(i=0;i<n;i++)
23+
{
24+
min=i;
25+
for(j=i;j<n;j++)
26+
{
27+
if(arr[min]>arr[j])
28+
min=j;
29+
}
30+
t=arr[i];
31+
arr[i]=arr[min];
32+
arr[min]=t;
33+
}
34+
System.out.println("The sorted array is: ");
35+
for(i=0;i<n;i++)
36+
System.out.print(arr[i]+" ");
37+
}
38+
}

day32/README.md

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,4 +47,42 @@ function selectionSort(arr){
4747
}
4848

4949
console.log ( selectionSort ([1, 5, 2, 7, 3, 4, 8, 9, 6]));
50+
```
51+
52+
## Java Implementation
53+
54+
### [Solution](./Java/selectionSort.java)
55+
56+
```java
57+
/**
58+
* @date 01/02/19
59+
* @author SPREEHA DUTTA
60+
*/
61+
import java.util.*;
62+
public class selectionSort {
63+
public static void main(String []args)
64+
{
65+
int n,i,j,t,min=0;
66+
Scanner sc=new Scanner(System.in);
67+
n=sc.nextInt();
68+
int arr[]=new int[n];
69+
for(i=0;i<n;i++)
70+
arr[i]=sc.nextInt();
71+
for(i=0;i<n;i++)
72+
{
73+
min=i;
74+
for(j=i;j<n;j++)
75+
{
76+
if(arr[min]>arr[j])
77+
min=j;
78+
}
79+
t=arr[i];
80+
arr[i]=arr[min];
81+
arr[min]=t;
82+
}
83+
System.out.println("The sorted array is: ");
84+
for(i=0;i<n;i++)
85+
System.out.print(arr[i]+" ");
86+
}
87+
}
5088
```

0 commit comments

Comments
 (0)