File tree Expand file tree Collapse file tree 3 files changed +99
-0
lines changed
Expand file tree Collapse file tree 3 files changed +99
-0
lines changed Original file line number Diff line number Diff line change 1+ import java .io .*;
2+ class Bubble
3+ {
4+ public static void main ()throws IOException
5+ {
6+ BufferedReader br =new BufferedReader (new InputStreamReader (System .in ));
7+ int i ,j ,t ,n ;
8+ System .out .println ("Enter the size of the array" );
9+ n =Integer .parseInt (br .readLine ());
10+ int a []=new int [n ];
11+ System .out .println ("Enter the array elements" );
12+ for (i =0 ;i <n ;i ++)
13+ a [i ]=Integer .parseInt (br .readLine ());
14+ System .out .println ("ORIGINAL ARRAY" );
15+ for (i =0 ;i <n ;i ++)
16+ System .out .print (a [i ]+" " );
17+ System .out .println ();
18+ for (i =0 ;i <n ;i ++)
19+ for (j =0 ;j <n -1 ;j ++)
20+ if (a [j ]>a [j +1 ])
21+ {
22+ t =a [j ];
23+ a [j ]=a [j +1 ];
24+ a [j +1 ]=t ;
25+ }
26+ System .out .println ("SORTED ARRAY" );
27+ for (i =0 ;i <n ;i ++)
28+ System .out .print (a [i ]+" " );
29+ }
30+ }
Original file line number Diff line number Diff line change 1+ import java .io .*;
2+ class Insertion
3+ {
4+ public static void main ()throws IOException
5+ {
6+ BufferedReader br =new BufferedReader (new InputStreamReader (System .in ));
7+ int n ,l ,key ,i ,j ;
8+ System .out .println ("Enter the size of the array" );
9+ n =Integer .parseInt (br .readLine ());
10+ int a []=new int [n ];
11+ System .out .println ("Enter the array elements" );
12+ for (i =0 ;i <n ;i ++)
13+ a [i ]=Integer .parseInt (br .readLine ());
14+ System .out .println ("ORGINAL ARRAY : " );
15+ for (i =0 ;i <n ;i ++)
16+ System .out .println (a [i ]);
17+
18+ //Sorting algorithm
19+ for (i =1 ;i <n ;i ++)
20+ {
21+ key = a [i ];
22+ j = i - 1 ;
23+ /* Move elements of arr[0..i-1], that are
24+ greater than key, to one position ahead
25+ of their current position */
26+ while (j >= 0 && a [j ] > key )
27+ {
28+ a [j + 1 ] = a [j ];
29+ j = j - 1 ;
30+ }
31+ a [j +1 ]=key ;
32+
33+ }
34+ //Displaying the array
35+ System .out .println ("SORTED ARRAY : " );
36+ for (i =0 ;i <n ;i ++)
37+ System .out .println (a [i ]);
38+ }
39+ }
Original file line number Diff line number Diff line change 1+ import java .io .*;
2+ class Selection
3+ {
4+ public static void main ()throws IOException
5+ {
6+ BufferedReader br =new BufferedReader (new InputStreamReader (System .in ));
7+ int i ,j ,t ,n ;
8+ System .out .println ("Enter the size of the array" );
9+ n =Integer .parseInt (br .readLine ());
10+ int a []=new int [n ];
11+ System .out .println ("Enter the array elements" );
12+ for (i =0 ;i <n ;i ++)
13+ a [i ]=Integer .parseInt (br .readLine ());
14+ System .out .println ("ORIGINAL ARRAY" );
15+ for (i =0 ;i <n ;i ++)
16+ System .out .print (a [i ]+" " );
17+ System .out .println ();
18+ for (i =0 ;i <n ;i ++)
19+ for (j =i +1 ;j <n ;j ++)
20+ if (a [i ]>a [j ])
21+ {
22+ t =a [i ];
23+ a [i ]=a [j ];
24+ a [j ]=t ;
25+ }
26+ System .out .println ("SORTED ARRAY" );
27+ for (i =0 ;i <n ;i ++)
28+ System .out .print (a [i ]+" " );
29+ }
30+ }
You can’t perform that action at this time.
0 commit comments