0% found this document useful (0 votes)
72 views

Import: Arr Temp Scan

This document contains the code for an implementation of merge sort in Java. It takes in an unsorted array of integers from the user, divides the array into smaller sub-problems, recursively sorts those sub-problems using merge sort, and then merges the sorted sub-problems back together to produce the fully sorted array. Key aspects of the algorithm include dividing the array in half recursively, merging sorted halves back together, and using additional temporary storage during the sorting process.

Uploaded by

sara amer
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
72 views

Import: Arr Temp Scan

This document contains the code for an implementation of merge sort in Java. It takes in an unsorted array of integers from the user, divides the array into smaller sub-problems, recursively sorts those sub-problems using merge sort, and then merges the sorted sub-problems back together to produce the fully sorted array. Key aspects of the algorithm include dividing the array in half recursively, merging sorted halves back together, and using additional temporary storage during the sorting process.

Uploaded by

sara amer
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

Merg_sort.

java

1 import java .util.Scanner;


2
3
4 public class Merg_sort {
5 int a [];
6 static int [] arr;
7 static int[] temp;
8 static Scanner scan = new Scanner (System.in) ;
9
10 public static void main (String args[]){
11 System.out.println("Hello I'm merge sort my operation basic is sorted array ");
12 System.out.println("Time efficiency : O(n log n) & space efficiency : O(n)");
13 System.out.println( "Pleas enter size array " );
14 System.out.println("");
15
16 int n=scan.nextInt(); // length array
17
18 int a []=new int [n]; //array
19
20 // add values to array
21
22 for(int i = 0 ; i<=n-1 ; i++){
23 System.out.println( "Pleas enter value "+ (i+1) + " into array" );
24 System.out.println("");
25
26 int s= scan .nextInt();
27 a[i]=s;
28 }
29 System.out.println( "befor sorting : " );
30 System.out.println("");
31
32 for(int k = 0 ; k<=n-1 ; k++)
33
34 System.out.println( "a["+k+"]"+ "=" + a[k] +" " );
35 System.out.println("");
36 System.out.println("");
37 print1(a);
38
39 }
40
41 /////// prepare for sort *
42 static void prepare_for_sort(int [] aa)
43 {
44 arr=aa;
45 temp= new int [aa.length];
46 do_merge_sort(0,aa.length-1);
47
48
49 }// end method prepare_for_sort
50
51
52
53 ////// divide to small problem *
54 static void do_merge_sort(int lower,int higher){
55
56 if (lower < higher)
57 {
58 int mid =lower+(higher-lower)/2;
59 do_merge_sort( lower,mid);
60 do_merge_sort( mid+1,higher);
61 merge_part(lower,mid,higher);
62 }// end if

Page 1
Merg_sort.java

63
64 }// end method do_merge_sort
65
66
67
68 /////// merge sort *
69 static void merge_part(int lower,int mid, int higher){
70
71 for(int i=lower; i<=higher ;i++)
72 {
73
74 temp[i] = arr [i];
75
76 }// end for
77
78 int i= lower;
79 int j= mid+1;
80 int k=lower;
81
82 while ( i<=mid && j<=higher )
83
84 {
85 if(temp[i]<=temp[j])
86 {
87 arr[k]=temp[i];
88 i++;
89 }
90 else
91 {
92 arr[k]=temp[j];
93 j++;
94 }
95
96 k++;
97
98 } // end while
99
100 while ( i<=mid )
101 {
102
103 arr[k]=temp[i];
104 k++;
105 i++;
106 } // end while
107
108
109 }// end method merge_part
110
111 // print method
112 static void print1(int [] a)
113 {
114 prepare_for_sort(a);
115 System.out.println( "after sorting " );
116 System.out.println("");
117
118 for(int w = 0 ; w< arr.length ; w++)
119
120 System.out.print( "a["+(w)+"]"+ "=" + arr[w] +" " );
121
122
123 }
124 }

Page 2
Merg_sort.java

125

Page 3

You might also like