Skip to content

Commit 240069f

Browse files
committed
assignment -2
1 parent cb66393 commit 240069f

6 files changed

Lines changed: 292 additions & 0 deletions

File tree

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
package getitdone.com;
2+
3+
public class BubbleSort {
4+
static void swap(int arr[],int i,int j) {
5+
int temp=arr[i];
6+
arr[i]=arr[j];
7+
arr[j]=temp;
8+
9+
}
10+
11+
public static void main(String[] args) {
12+
int[] arr= {4,2,5,2,1};
13+
for(int i=0;i<arr.length;i++) {
14+
for(int j=1;j<arr.length-i;j++) {
15+
if(arr[j]<arr[j-1]) {
16+
swap(arr,j,j-1);
17+
}
18+
}
19+
}
20+
for(int i=0;i<arr.length;i++) {
21+
System.out.print(arr[i]+" ");
22+
}
23+
24+
}
25+
26+
}
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
package getitdone.com;
2+
3+
import java.util.Arrays;
4+
5+
public class CheckIfAnArrayIsSubsetOfAnotherArray {
6+
static boolean isSubset(int arr1[],int arr2[],int m,int n) {
7+
int i=0;
8+
int j=0;
9+
if (m < n)
10+
return false;
11+
12+
Arrays.sort(arr1); // sorts arr1
13+
Arrays.sort(arr2); // sorts arr2
14+
15+
while (i < n && j < m) {
16+
if (arr1[j] < arr2[i])
17+
j++;
18+
else if (arr1[j] == arr2[i]) {
19+
j++;
20+
i++;
21+
}
22+
else if (arr1[j] > arr2[i])
23+
return false;
24+
}
25+
26+
if (i < n)
27+
return false;
28+
else
29+
return true;
30+
31+
}
32+
33+
public static void main(String[] args) {
34+
int arr1[] = { 11, 1, 13, 21, 3, 7 };
35+
int arr2[] = { 11, 7, 1 };
36+
37+
int m = arr1.length;
38+
int n = arr2.length;
39+
40+
if (isSubset(arr1, arr2, m, n))
41+
System.out.println("arr2 is a subset of arr1");
42+
else
43+
System.out.println(
44+
"arr2 is not a subset of arr1");
45+
46+
}
47+
48+
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
package getitdone.com;
2+
3+
import java.util.Arrays;
4+
5+
public class FindDuplicate {
6+
7+
public static void main(String[] args) {
8+
int arr[]= {3,1,4,6,7};
9+
if(isDuplicatePresent(arr)) {
10+
System.out.println("Duplicate elements are present ");
11+
}else {
12+
System.out.println("No duplicates are present");
13+
}
14+
15+
}
16+
17+
private static boolean isDuplicatePresent(int[] arr) {
18+
Arrays.sort(arr);
19+
for(int i=1;i<arr.length;i++) {
20+
if(arr[i]==arr[i-1]) {
21+
return true;
22+
}
23+
}
24+
25+
return false;
26+
}
27+
28+
}
Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
package getitdone.com;
2+
3+
import java.util.Scanner;
4+
5+
public class MergeSort {
6+
static void merge(int[] arr,int l,int mid,int h) {
7+
int n1=mid-l+1;
8+
int n2=h-mid;
9+
int[] left=new int[mid-l+1];
10+
int[] right=new int[h-mid];
11+
for(int i=0;i<left.length;i++) {
12+
left[i]=arr[l+i];
13+
}
14+
for( int i=0;i<right.length;i++) {
15+
right[i]=arr[mid+i+1];
16+
}
17+
int i=0,j=0,k=l;
18+
while(i<n1 && j<n2) {
19+
if(left[i]<=right[j]) {
20+
arr[k]=left[i];
21+
i++;
22+
k++;
23+
}
24+
else {
25+
arr[k]=right[j];
26+
j++;
27+
k++;
28+
}
29+
}
30+
while(i<n1) {
31+
arr[k]=left[i];
32+
i++;
33+
k++;
34+
35+
}
36+
while(j<n2) {
37+
arr[k]=right[j];
38+
j++;
39+
k++;
40+
41+
}
42+
43+
}
44+
45+
public static void mergeSort(int arr[],int l,int h){
46+
47+
48+
if(l<h) {
49+
int mid=l+(h-l)/2;
50+
mergeSort(arr,l,mid);
51+
mergeSort(arr,mid+1,h);
52+
merge(arr,l,mid,h);
53+
}
54+
}
55+
56+
public static void main(String[] args) {
57+
Scanner s =new Scanner(System.in);
58+
System.out.println("Enter the size of an array");
59+
int n=s.nextInt();
60+
int[] arr=new int[n];
61+
System.out.println(" Enter the values");
62+
for(int i=0;i<n;i++) {
63+
arr[i]=s.nextInt();
64+
}
65+
System.out.println(" This is the given array");
66+
for(int i=0;i<n;i++) {
67+
System.out.print(arr[i] +" ");
68+
}System.out.println();
69+
int start=0;
70+
int end=arr.length-1;
71+
mergeSort(arr,start,end);
72+
73+
System.out.println("This is the sorted array");
74+
for(int i=0;i<n;i++) {
75+
System.out.print(arr[i] +" ");
76+
}
77+
78+
}
79+
80+
}
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
package getitdone.com;
2+
3+
import java.util.Scanner;
4+
5+
public class QuickSort {
6+
static void swap(int arr[],int i,int j) {
7+
int temp=arr[i];
8+
arr[i]=arr[j];
9+
arr[j]=temp;
10+
11+
}
12+
static int partition(int[] arr, int lo,int hi) {
13+
int i=lo-1;
14+
int pivote=arr[hi];
15+
for(int j=lo;j<arr.length;j++) {
16+
if(arr[j]<pivote) {
17+
i++;
18+
swap(arr,i,j);
19+
}
20+
}
21+
swap(arr,i+1,hi);
22+
23+
24+
25+
return i+1;
26+
27+
}
28+
29+
private static void quickSort(int arr[],int l,int h) {
30+
int n=arr.length;
31+
if(l<h) {
32+
int p=partition(arr,l,h);
33+
quickSort(arr,l,p-1);
34+
quickSort(arr,p+1,h);
35+
}
36+
}
37+
38+
public static void main(String[] args) {
39+
Scanner s =new Scanner(System.in);
40+
System.out.println("Enter the size of an array");
41+
int n=s.nextInt();
42+
int[] arr=new int[n];
43+
System.out.println(" Enter the values");
44+
for(int i=0;i<n;i++) {
45+
arr[i]=s.nextInt();
46+
}
47+
System.out.println(" This is the given array");
48+
for(int i=0;i<n;i++) {
49+
System.out.print(arr[i] +" ");
50+
}System.out.println();
51+
52+
int low=0;
53+
int high=n-1;
54+
quickSort(arr,low,high);
55+
56+
System.out.println("This is the sorted array");
57+
for(int i=0;i<n;i++) {
58+
System.out.print(arr[i] +" ");
59+
}
60+
61+
}
62+
63+
}
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
package getitdone.com;
2+
3+
import java.util.Scanner;
4+
5+
public class SelectionSort {
6+
static void swap(int arr[],int i,int j) {
7+
int temp=arr[i];
8+
arr[i]=arr[j];
9+
arr[j]=temp;
10+
11+
}
12+
static void selectionSort(int[] arr) {
13+
for(int i=0;i<arr.length;i++) {
14+
int minIndex=i;
15+
for(int j=i+1;j<arr.length;j++) {
16+
if(arr[j]<arr[minIndex]) {
17+
minIndex=j;
18+
19+
}
20+
21+
}
22+
swap(arr,i,minIndex);
23+
}
24+
}
25+
26+
public static void main(String[] args) {
27+
Scanner s =new Scanner(System.in);
28+
System.out.println("Enter the size of an array");
29+
int n=s.nextInt();
30+
int[] arr=new int[n];
31+
System.out.println(" Enter the values");
32+
for(int i=0;i<n;i++) {
33+
arr[i]=s.nextInt();
34+
}
35+
System.out.println(" This is the given array");
36+
for(int i=0;i<n;i++) {
37+
System.out.print(arr[i] +" ");
38+
}System.out.println();
39+
selectionSort(arr);
40+
41+
System.out.println("This is the sorted array");
42+
for(int i=0;i<n;i++) {
43+
System.out.print(arr[i] +" ");
44+
}
45+
}
46+
47+
}

0 commit comments

Comments
 (0)