Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

JAVA : question 1 The selection sort is a combination of searching and sorting.

ID: 3582218 • 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

public static void selectionSort(int[] list) {
   int index;
   int temp;
   for(int i=0; i<list.length-1; ++i) {
       index = minIndex(list, i, list.length);
       temp = list[index];
       list[index] = list[i];
       list[i] = temp;
   }
}

private static int minIndex(int[] list, int startIndex, int endIndex) {
   int index = startIndex;
   for(int i=startIndex+1; i<endIndex; ++i)
       if (list[i]<list[index])
           index = i;
   return index;
}

//for second question, create 4 files: MyStackInterface, MyData, one tester file for main function, and the following MyStack

public class MyStack implements MyStackInterface {

   MyData top = null;

   public boolean isEmpty() {
       return (top==null);
   }

   public void push(MyData data) {
       if (top==null)
           top = data;
       else {
           data.setNext(top);
           top = data;
       }
   }

   public MyData top() {return top;}

   public void pop() {
       if (top==null)
           return;
       else {
           top = top.getNext();
       }
   }
}