Question

In: Computer Science

Write a java program that randomizes a list and sorts it. The list will be randomized...

Write a java program that randomizes a list and sorts it.

  1. The list will be randomized to be at least 12 elements and no more than 30 elements with values between 1 and 100.
  2. You will submit one program with two sorts done separately within the program. The list will be re-randomized before reach sort.
  3. Write a flowchart for each sort.

Solutions

Expert Solution

package sort;

import java.util.Random;

/*To avoid running inner loop even though array is sorted

* We can pass swapped flag inside if statement

* Initialize flag to false as not element has swapped before start swapping

* every time element get swapped set swapped flag to true

* after one iteration not single element swapped ie flag is false

* means array is swapped already no need to make another iteration

* get out of the loop

* this algorithm wont need (n^2) in best case

* in best case it perform result in even O(N) time

*/

public class BubbleSort {

public static void bubbleSort(int ar[]){

for(int i=0;i<ar.length;i++){

boolean swapped=false; //for each iteration set flag =false

for(int j=1;j<ar.length;j++){

if(ar[j-1]>ar[j]){

int temp=ar[j-1];

ar[j-1]=ar[j];

ar[j]=temp;

swapped=true; //if swapped flag =true

}

}

if(!swapped) //if not element swapped at all

break; //array is sorted get out of loop

}

}

public static int[] getRandomNumberArray(int arraySize ){

int ar[]=new int[arraySize];

Random rnd = new Random();

for(int i=0;i<ar.length;i++){

ar[i]= rnd.nextInt(100);

}

return ar;

}

public static void selSort(int[] arr){

  

for (int i=0;i<arr.length;i++)

{

   // int smallerNumber = arr[i]; //make current element as smallest

   int index=i; //initialize smallest i as -1

  

for(int j=i+1;j<arr.length;j++){ //find smallest element in ar[i.....n]

   if(arr[index]>arr[j]){

  

   index=j; //remember i of smallest

   }

   }

   if(index!=-1){ //if smallest found

   int smallerNumber=arr[i]; //swap it

   arr[i]=arr[index];

   arr[index] = smallerNumber;

   }

}

  

}

public static void main(String[] args) {

Random r=new Random();

int h=30;

int l=12;

int size=r.nextInt(h-l) + l; //no of element between 12 to 30

System.out.println("Normal array");

int ar[]=getRandomNumberArray(size);

for(int i=0;i<size;i++){

System.out.print(ar[i]+" , ");

}

System.out.println();

System.out.println("Bubble sort");

bubbleSort(ar);

for(int i=0;i<size;i++){

System.out.print(ar[i]+" , ");

}

System.out.println();

System.out.println("selection sort");

ar=getRandomNumberArray(size);

selSort(ar);

for(int i=0;i<size;i++){

System.out.print(ar[i]+" , ");

}

}

}

output

Normal array

95 , 51 , 74 , 81 , 31 , 69 , 92 , 38 , 32 , 37 , 19 , 75 , 3 , 79 , 99 , 24 , 3 , 74 ,

Bubble sort

3 , 3 , 19 , 24 , 31 , 32 , 37 , 38 , 51 , 69 , 74 , 74 , 75 , 79 , 81 , 92 , 95 , 99 ,

selection sort

3 , 6 , 7 , 21 , 24 , 30 , 41 , 43 , 49 , 51 , 52 , 63 , 71 , 73 , 81 , 89 , 93 , 98 ,

selection sort flowchart

Bubble Sort flowchar


Related Solutions

Write a Java program that sorts an array of “Student” in an aescending order of their...
Write a Java program that sorts an array of “Student” in an aescending order of their “last names”. The program should be able to apply (Insertion sort): Student [] studs = new Student[8]; s[0] = new Student("Saoud", "Mohamed", 3.61); s[1] = new Student("Abdelkader", "Farouk", 2.83); s[2] = new Student("Beshr" , "Alsharqawy", 1.99); s[3] = new Student("Nader", "Salah", 3.02); s[4] = new Student("Basem", "Hawary", 2.65); s[5] = new Student("Abdullah", "Babaker", 2.88); s[6] = new Student("Abdelaal", "Khairy", 3.13); s[7] = new Student("Mohamedain",...
Write a MIPS program to implement the Bubble Sort algorithm, that sorts an input list of...
Write a MIPS program to implement the Bubble Sort algorithm, that sorts an input list of integers by repeatedly calling a “swap” subroutine. The original unsorted list of integers should be received from the keyboard input. Your program should first prompt the user “Please input an integer for the number of elements:”. After the user enters a number and return, your program outputs message “Now input each element and then a return:”. For example, if the user enters 8 as...
Write a MIPS program using the Bubble Sort algorithm, that sorts an input list of integers...
Write a MIPS program using the Bubble Sort algorithm, that sorts an input list of integers by repeatedly calling a “swap” subroutine. The original unsorted list of integers should be received from the keyboard input. Your program should first prompt the user “Please input an integer for the number of elements:”. After the user enters a number and return, your program outputs message “Now input each element and then a return:”. For example, if the user enters 5 as the...
Write a Java program that reads a list of integers into an array. The program should...
Write a Java program that reads a list of integers into an array. The program should read this array from the file “input.txt”. You may assume that there are fewer than 50 entries in the array. Your program determines how many entries there are. The output is a two-column list. The first column is the list of the distinct array elements; the second column is the number of occurrences of each element. The list should be sorted on entries in...
Write a java program: Write a nested loop program creating an array list of three positions.  The...
Write a java program: Write a nested loop program creating an array list of three positions.  The first loop terminates with a sentinel value of which user is prompted for input to continue of quit.   Inside loop to enter in each position a prompted input of person's first name.  After the inside loop, edit the second array location making its value "Boston". Print the array's contents.
We have a list of runner and their running time, write a program in Java to...
We have a list of runner and their running time, write a program in Java to show the fastest runner and their time.
Write a Java program that prompts the user to enter a list of integer values and...
Write a Java program that prompts the user to enter a list of integer values and displays whether the list is sorted in increasing order or not. Here is a sample run. Note that the first number in the input indicates the number of the elements in the list. <Output> Enter list: 8 101516619111 The list is not sorted <End Output <Output> Enter list: 10 11344579 11 21 The list is already sorted <End Output Create a complete class for...
Write a Java program to implement a Single Linked List that will take inputs from a...
Write a Java program to implement a Single Linked List that will take inputs from a user as Student Names. First, add Brian and Larry to the newly created linked list and print the output Add "Kathy" to index 1 of the linked list and print output Now add "Chris" to the start of the list and "Briana" to the end of the list using built-in Java functions. Print the output of the linked list.
Write a Java program to implement a double-linked list with addition of new nodes at the...
Write a Java program to implement a double-linked list with addition of new nodes at the end of the list. Add hard coded nodes 10, 20, 30, 40 and 50 in the program. Print the nodes of the doubly linked list.
write a program using Java language that is- Implement Stack with a linked list, and demonstrate...
write a program using Java language that is- Implement Stack with a linked list, and demonstrate that it can solve the Tower of Hanoi problem. Write implementation body of method “infixToPrefix(String[] e)” of class ArithmeticExpression to convert infix expressions into prefix expressions.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT