In: Computer Science
In Java please
Your program will sort a two dimensional array (5 * 4) based on the following:
Ask the user for a number, search for that number in the 5th row of the array that was sorted via insertion sort, using binary search. Display the entire column.
Your array could be declared as a global variable since it is being used everywhere.
For example, given the following array:
5 |
3 |
2 |
16 |
9 |
8 |
10 |
17 |
4 |
7 |
11 |
18 |
2 |
5 |
9 |
12 |
7 |
9 |
4 |
10 |
5 |
3 |
2 |
16 |
|
9 |
8 |
10 |
17 |
|
4 |
7 |
11 |
18 |
|
2 |
5 |
9 |
12 |
|
5th row Insertion Ascending |
7 |
9 |
4 |
10 |
1st column Bubble Ascending |
2nd column Selection Descending |
3rd column Shell Ascending |
After bubble sort
2 |
5 |
9 |
12 |
4 |
7 |
11 |
18 |
5 |
3 |
2 |
16 |
7 |
9 |
4 |
10 |
9 |
8 |
10 |
17 |
After selection sort (Descending order)
7 |
9 |
4 |
10 |
9 |
8 |
10 |
17 |
4 |
7 |
11 |
18 |
2 |
5 |
9 |
12 |
5 |
3 |
2 |
16 |
Do the same kind of thing for shell sort (Ascending order based on the 3rd column)
After Insertion sort
2 |
5 |
3 |
16 |
10 |
9 |
8 |
17 |
11 |
4 |
7 |
18 |
9 |
2 |
5 |
12 |
4 |
7 |
9 |
10 |
What number are you searching for in the 5th row? 9
3 |
8 |
7 |
5 |
9 |
Make sure to modularize your program. Each of the sorts and searches must happen in their own functions. Reset the array to its original contents after each sort.
int[][] mat=new int[5][4];
for(int i=0;i<5;i++)
{
for(int j=0;j<4;j++){
mat[i][j]=sc.nextInt();
}
}
a)BUBBLE SORT
for
(
int
i =
0
; i < 5-
1
;
i++)
for
(
int
j =
0
; j < 5-i-
1
;
j++)
if
(mat[j][0] >
mat[j+
1
][0])
{
swap(mat[j][0],mat[j+1][0]);
swap(mat[j][1],mat[j+1][1]);
swap(mat[j][2],mat[j+1][2]);
swap(mat[j][3],mat[j+1][3]);
}
b)SELECTION SORT
for
(
int
i =
0
; i < 5-
1
;
i++)
{
//
Find the minimum element in unsorted array
int
min_idx = i;
for
(
int
j =
i+
1
; j < 5; j++)
if
(mat[j][]<mat[min_idx][])
min_idx
= j;
swap(mat[min_idx][0],mat[i][0]);
swap(mat[min_idx][1],mat[i][1]);
swap(mat[min_idx][2],mat[i][2]);
swap(mat[min_idx][3],mat[i][3]);
}
Please please give me thumbs uo please