JAVA : question 1 The selection sort is a combination of searching and sorting.
ID: 3582211 • Letter: J
Question
JAVA : question 1
The selection sort is a combination of searching and sorting. During each pass, the unsorted
element with the smallest (or largest) value is moved to its proper position in the array.
Create a file named Problem1.java (must be named as Problem1.java), then implement the following two methods:
selectionSort and minIndex methods.
public static void selectionSort(int[] list){
//this method is used to sort array in ascending order, and you MUST use the minIndex method within this method
//add code here
}
private static int minIndex(int[] list, int startIndex, int endIndex){
//this method returns the index of the smallest value among the elements as specified
//by startIndex and endIndex. Add your code below
}
Problem 2 (40 points)
The typical operations of a stack ADT include push, top, and pop. The push operation adds an element to the top of the stack;
the pop operation removes the top element of the stack; the top operation returns the top element of the stack.
Write a class named MyStack.java, which implements MyStackInterface, then use the following code to test your stack ADT
(Node: you are not allowed to use any predefined data structures)
public static void main(String[] args){
MyStack s=new MyStack();
s.push(new MyData("1"));
s.push(new MyData("2"));
s.push(new MyData("3"));
while(!s.isEmpty()){
System.out.println((String)(s.top().getData()));
s.pop();
}
}
public class MyData{
private Object data;
private MyData next;
public MyData(Object data){
setData(data);
next=null;
}
public Object getData(){
return data;
}
public MyData getNext(){
return next;
}
public void setData(Object data){
this.data = data;
}
public void setNext(MyData next){
this.next = next;
}
}
interface MyStackInterface{
public boolean isEmpty();
public void push(MyData data);
public MyData top();
public void pop();
}
Explanation / Answer
import java.util.Scanner;
public class Problem1 {
/*
* Java Program to Implement Selection Sort
*/
/* Class SelectionSort */
static class SelectionSort
{
/* Selection Sort function */
public static void sort( int list[] ){
int N = list.length;
int i, j, pos, temp;
for (i = 0; i < N-1; i++)
{
pos = i;
for (j = i+1; j < N; j++)
{
if (list[j] < list[pos])
{
pos = j;
}
}
/* Swap list[i] and list[pos] */
temp = list[i];
list[i] = list[pos];
list[pos]= temp;
}
}
/* Main method */
public static void main(String[] args)
{
Scanner scan = new Scanner( System.in );
System.out.println("Selection Sort Test ");
int n, i;
/* Accept number of elements */
System.out.println("Enter number of integer elements");
n = scan.nextInt();
/* Create integer array on n elements */
int list[] = new int[ n ];
/* Accept elements */
System.out.println(" Enter "+ n +" integer elements");
for (i = 0; i < n; i++)
list[i] = scan.nextInt();
/* Call method sort */
sort(list);
/* Print sorted Array */
System.out.println(" Elements after sorting ");
for (i = 0; i < n; i++)
System.out.print(list[i]+" ");
System.out.println();
}
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.