Data Structures & C++: Write a program to solve the selection problem. Let k=N/2
ID: 3882176 • Letter: D
Question
Data Structures & C++:
Write a program to solve the selection problem. Let k=N/2. Print out a table showing the running time of your program for various values of N.
- Declare the array and initialize with the values in group of elements.
-implement bubble sort using For outer to swap the values inside the array.
-sort the elements in the array.
-DIsplay the Kth element in the array as output
According to my book, I need to: " read the N numbers into an array, sort the array in decreasing order by some simple algorithm such as bubble sort, and then return the element in position k.". I've never used bubble sort and confused by the process, any help is greatly appreciated.
Sample Output: N value N/2nd Largest element Running Time (nsec) 1000 2000 3000 4000 5000 6000 7008 8000 9000 505 965 1495 1963 2327 2787 3277 3947 4135 4521 8 37 94 130 172 224 285 Press any key to continueExplanation / Answer
import java.util.Arrays;
import java.util.ArrayList;
public class selection {
public static void main(String[] args) {
int N[] = {5,7,3,4,8,10};
int[] S = null;
int i = 0;
int prev = 0;
while (i < N.length) {
if (N[i] < prev) {
S[prev] = N[i];
} else {
N[i] = S[i];
}
prev = N[i];
i++;
System.out.println(prev);
}
for (int j = 0; j < N.length; j++) {
System.out.print(S[j] + ",");
}
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.