) Write a program where you use Selection Sort to sort the following array in as
ID: 3676815 • Letter: #
Question
) Write a program where you use Selection Sort to sort the following array in ascending order.
24,2,45,20,56,75,2,56,99,53,12
a) Declare an array of integers called unsortedarr and load it with the integers above. Notice we have 11 numbers, and the number 2 is repeated which is fine.
Display the array as it is by printing it before the sort. b) Sort the array using the insertion sort technique and display the array after you sort it.
Here is the beginning of the code, complete it, run it, test it, submit it.
import java.util.*;
public class MySelectSort { public static void main(String[] args) { int arr[] = {24,2,45,20,56,75,2,56,99,53,12}; System.out.println("-------------------------- unsorted"); System.out.println(arr);
// continue … write your code
Explanation / Answer
Hey heres the java code for selection sort,I thought to continue with your code without making any changes as you mentioned:
import java.util.*;
public class samplel {
public static void main(String[] args) {
int arr[] = {24,2,45,20,56,75,2,56,99,53,12};
System.out.println("-------------------------- unsorted");
System.out.println(arr); //this line prints reference of arr variable not value, so you can remove it.
int temp,min,i,j;
for(i=0;i<11;i++)
{
min=i;
for(j=i+1;j<11;j++)
{
if(arr[j]<arr[min])
min=j;
}
temp=arr[min];
arr[min]=arr[i];
arr[i]=temp;
}
System.out.println("Sorted List is ");
for(i=0;i<11;i++)
{
System.out.println(arr[i]);
}
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.