100 Days of Code (48 Part Series)
1 Day 1: Two Sum
2 Day 2: Check Prime
… 44 more parts…
3 Day 3: Hex Colour
4 Day 4: Repeating Numbers
5 Day 5: Nearest Prime Numbers
6 Day 6: Anagram Detector
7 Day 7: Poker ID
8 Day 8: Bubble Sort
9 Day 9: Password Generator
10 Day 10: Array Search
11 Day 11: Rock, Paper, Scissors
12 Day 12: List Shuffle
13 Day 13: Validate ISBN
14 Day 14: HTML Markup Generator
15 Day 15: Pascal’s Triangle
16 Day 16: Pascal’s Pyramid
17 Day 17: d20 Dice Roller
18 Day 18: Base64
19 Day 19: Vigenère cipher
20 Day 20: Seven-segment display
21 Day 21: Insertion Sort
22 Day 22: Binary-Decimal
23 Day 23: Ducci sequence
24 Day 24: Shell Sort
25 Day 25: Calculator
26 Day 26: Merge Sort
27 Day 27: Bucket Sort
28 Day 28: Counting Sort
29 Day 29: Heap Sort
30 Day 30: Quick Sort
31 Day 31: Radix Sort
32 Day 32: Shaker Sort
33 Day 33: Stooge Sort
34 Day 34: Consuming a RESTful API with React
35 Day 35: Adding Machine
36 Day 36: React Form Validation
37 Day 37: Time Format Converter
38 Day 38: React Stopwatch
39 Day 39: Blackjack Checker
40 Day 40: Password Guessing Game
41 Day 41: String Case
42 Day 42: Tip Calculator
43 Day 43: Twitter Auto-reply
44 Day 44: React Currency Converter
45 Day 45: Phonebook
46 Day 46: Sudoku Generator
47 Day 47: Binary Search Tree
48 Day 48: AVL Tree
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.Arrays;
public class MergeSort {
// Function to merge the two sorted arrays
static void merge(int[] array,int low,int mid,int high){
int i,j,k;
int[] c= new int[high-low+1];
k = 0;
i=low;
j=mid+1;
while(i<=mid && j<=high){
if(array[i]<=array[j]){
c[k++] = array[i++];
}
else{
c[k++] = array[j++];
}
}
while(i<=mid){
c[k++] = array[i++];
}
while(j<=high){
c[k++] = array[j++];
}
k=0;
for(i = low; i<=high; i++){
array[i] = c[k++];
}
}
// Function implementing the merge sort
static void mergeSort(int[] array,int low, int high){
if(high-low+1>1){
int mid = (low+high)/2;
mergeSort(array,low,mid);
mergeSort(array,mid+1,high);
merge(array,low,mid,high);
}
}
// Function to read the input and display the output
public static void main(String[] args) {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
int size;
System.out.println("Enter the size of the array");
try {
size = Integer.parseInt(br.readLine());
} catch (Exception e) {
System.out.println("Invalid Input");
return;
}
int[] array = new int[size];
System.out.println("Enter array elements");
int i;
for (i = 0; i < array.length; i++) {
try {
array[i] = Integer.parseInt(br.readLine());
} catch (Exception e) {
System.out.println("An error Occurred");
}
}
System.out.println("The initial array is");
System.out.println(Arrays.toString(array));
mergeSort(array,0,array.length-1);
System.out.println("The sorted array is");
System.out.println(Arrays.toString(array));
}
}
Enter fullscreen mode Exit fullscreen mode
100 Days of Code (48 Part Series)
1 Day 1: Two Sum
2 Day 2: Check Prime
… 44 more parts…
3 Day 3: Hex Colour
4 Day 4: Repeating Numbers
5 Day 5: Nearest Prime Numbers
6 Day 6: Anagram Detector
7 Day 7: Poker ID
8 Day 8: Bubble Sort
9 Day 9: Password Generator
10 Day 10: Array Search
11 Day 11: Rock, Paper, Scissors
12 Day 12: List Shuffle
13 Day 13: Validate ISBN
14 Day 14: HTML Markup Generator
15 Day 15: Pascal’s Triangle
16 Day 16: Pascal’s Pyramid
17 Day 17: d20 Dice Roller
18 Day 18: Base64
19 Day 19: Vigenère cipher
20 Day 20: Seven-segment display
21 Day 21: Insertion Sort
22 Day 22: Binary-Decimal
23 Day 23: Ducci sequence
24 Day 24: Shell Sort
25 Day 25: Calculator
26 Day 26: Merge Sort
27 Day 27: Bucket Sort
28 Day 28: Counting Sort
29 Day 29: Heap Sort
30 Day 30: Quick Sort
31 Day 31: Radix Sort
32 Day 32: Shaker Sort
33 Day 33: Stooge Sort
34 Day 34: Consuming a RESTful API with React
35 Day 35: Adding Machine
36 Day 36: React Form Validation
37 Day 37: Time Format Converter
38 Day 38: React Stopwatch
39 Day 39: Blackjack Checker
40 Day 40: Password Guessing Game
41 Day 41: String Case
42 Day 42: Tip Calculator
43 Day 43: Twitter Auto-reply
44 Day 44: React Currency Converter
45 Day 45: Phonebook
46 Day 46: Sudoku Generator
47 Day 47: Binary Search Tree
48 Day 48: AVL Tree
原文链接:Day 26: Merge Sort
暂无评论内容