Skip to content

Commit 0794f71

Browse files
Add files via upload
1 parent 9f37c62 commit 0794f71

File tree

4 files changed

+159
-0
lines changed

4 files changed

+159
-0
lines changed

RECURSION 2/MERGE SORT.txt

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
public class solution {
2+
3+
public static void mergeSort(int[] input){
4+
// Write your code here
5+
6+
if(input.length<=1)
7+
return;
8+
int mid=input.length/2;
9+
int[] part1=new int[mid];
10+
int[] part2=new int[input.length-mid];
11+
for(int i=0;i<mid;i++)
12+
{
13+
part1[i]=input[i];
14+
}
15+
int k=0;
16+
for(int i=mid;i<input.length;i++){
17+
part2[k]=input[i];
18+
k++;
19+
}
20+
mergeSort(part1);
21+
mergeSort(part2);
22+
merge(input,part1,part2);
23+
}
24+
private static void merge(int[] input,int part1[],int part2[]){
25+
26+
int i=0,j=0,k=0;
27+
while(i<part1.length && j<part2.length){
28+
if(part1[i]<part2[j])
29+
{
30+
input[k]=part1[i];
31+
k++;
32+
i++;
33+
}
34+
else {
35+
input[k]=part2[j];
36+
k++;
37+
j++;
38+
}
39+
}
40+
while(i<part1.length)
41+
{
42+
input[k]=part1[i];
43+
k++;
44+
i++;
45+
}
46+
while(j<part2.length){
47+
input[k]=part2[j];
48+
k++;
49+
j++;
50+
}
51+
52+
53+
54+
}
55+
}

RECURSION 2/QUICK SORT.txt

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
/*
2+
To apply the method explained in the hint video, we have to add two more parameters in the function call. This can be done by calling a helper function from given function. The helper function can have three parameters: array, start index, end index.
3+
4+
Skeleton code to achieve this:
5+
public class Solution {
6+
public static void quickSort(int[] input, int startIndex, int endInedx) {
7+
// your code goes here
8+
}
9+
10+
public static void quickSort(int[] input) {
11+
quickSort(input, 0, input.length - 1);
12+
}
13+
14+
}
15+
16+
*/
17+
18+
19+
public class Solution {
20+
21+
public static void quickSort(int[] input) {
22+
/* Your class should be named Solution
23+
* Don't write main().
24+
* Don't read input, it is passed as function argument.
25+
* No need to print or return the output.
26+
* Taking input and printing output is handled automatically.
27+
*/
28+
quickSort(input,0,input.length-1);
29+
}
30+
public static void quickSort(int[] input,int sI,int eI){
31+
//base case
32+
if(sI>=eI)
33+
return;
34+
35+
int pivotPos=partition(input,sI,eI);
36+
quickSort(input,sI,pivotPos-1);
37+
quickSort(input,pivotPos+1,eI);
38+
}
39+
public static int partition(int input[],int sI,int eI)
40+
{
41+
int pivot=input[sI];
42+
int count=0;
43+
for(int i=sI+1;i<=eI;i++)
44+
{
45+
if(input[i]<=pivot)
46+
count++;
47+
}
48+
int pivotPos=sI+count;
49+
int temp=input[sI];
50+
input[sI]=input[pivotPos];
51+
input[pivotPos]=temp;
52+
int i=sI;
53+
int j=eI;
54+
while(i<pivotPos && j>pivotPos)
55+
{
56+
if(input[i]<=input[pivotPos])
57+
i++;
58+
else if(input[j]>input[pivotPos])
59+
j--;
60+
else
61+
{
62+
int temp_=input[i];
63+
input[i]=input[j];
64+
input[j]=temp_;
65+
i++;j--;
66+
67+
}
68+
}
69+
return pivotPos;
70+
}
71+
72+
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
public class Solution {
2+
3+
public static String removeConsecutiveDuplicates(String s) {
4+
// Write your code here
5+
if(s.length()<=1)
6+
return s;
7+
String smallans=removeConsecutiveDuplicates(s.substring(1));
8+
if(smallans.charAt(0)==s.charAt(0))
9+
return smallans;
10+
else
11+
return s.charAt(0)+smallans;
12+
}
13+
14+
}

RECURSION 2/REMOVE X.txt

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
2+
public class solution {
3+
4+
// Return the changed string
5+
public static String removeX(String input){
6+
// Write your code here
7+
if(input.length()==0)
8+
return input;
9+
10+
if(input.charAt(0)=='x')
11+
return removeX(input.substring(1));
12+
13+
else
14+
return input.charAt(0)+removeX(input.substring(1));
15+
16+
17+
}
18+
}

0 commit comments

Comments
 (0)