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: 3582209 • 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

Problem1.java

public static void main (String[] args) throws java.lang.Exception
   {
   int[] arr1 = {9,14,3,2,43,11,58,22};
       selectionSort(arr1);   
   }
private static int minIndex(int[] list, int startIndex, int endIndex)
{
int index = startIndex;
for (int j = startIndex + 1; j <endIndex; j++)
{
if (list[j] < list[index])
{
index = j;//searching for lowest index
}
}
return index;
}

public static void selectionSort(int[] list)
{
for (int i = 0; i < list.Count(); i++)
{
int index = minIndex(list, i, list.Count() - 1);
int smallerNumber = list[i];
list[i] = list[index];
list[index] = smallerNumber;
}
for (int i = 0; i < list.Count(); i++)
{
System.out.println(list[i]);
}
}

Problem2:

public class MyStack implements MyStackInterface
{
List<MyData> obj = new List<MyData>();
public boolean isEmpty()
{
if (obj != null)
{
return true;
}
return false;
}
public void push(MyData data)
{
obj.Add(data);
}
public MyData top()
{
MyData tempObj = obj[obj.Count - 1];
return tempObj;
}
public void pop()
{
obj.RemoveAt(obj.Count - 1);
}
}