Hello, I was hoping you can help me figure out how to make this Bubble Sort go from Ascending to Descending order (instead of 1,2,3 it needs to be 3,2,1) import java.text.DecimalFormat;import java.util.Random; public class Question4 {static final int SIZE = 50; // size of array to be sortedstatic int[] values = new int[SIZE]; // values to be sorted static void initValues()// Initializes the values array with random integers from 0 to 99.{Random rand = new Random();for (int index = 0; index < SIZE; index++)values[index] = Math.abs(rand.nextInt()) % 100;} static public boolean isSorted()// Returns true if the array values are sorted and false otherwise.{for (int index = 0; index < (SIZE - 1); index++)if (values[index] > values[index + 1])return false;return true;} static public void swap(int index1, int index2)// Precondition: index1 and index2 are >= 0 and < SIZE.//// Swaps the integers at locations index1 and index2 of the values array.{int temp = values[index1];values[index1] = values[index2];values[index2] = temp;} static public void printValues()// Prints all the values integers.{int value;DecimalFormat fmt = new DecimalFormat("00");System.out.println("The values array is:");for (int index = 0; index < SIZE; index++){value = values[index];if (((index + 1) % 10) == 0)System.out.println(fmt.format(value));elseSystem.out.print(fmt.format(value) + " ");}System.out.println();}///////////////////////////////////////////////////////////////////// Bubble Sort static void bubbleUp(int startIndex, int endIndex)// Switches adjacent pairs that are out of order// between values[startIndex]..values[endIndex]// beginning at values[endIndex].{for (int index = endIndex; index > startIndex; index--)if (values[index] < values[index - 1])swap(index, index - 1);}static void bubbleSort()// Sorts the values array using the bubble sort algorithm.{int current = 0;while (current < (SIZE - 1)){bubbleUp(current, SIZE - 1);current++;}}public static void main(String[] args){initValues();printValues();System.out.println("values is sorted: " + isSorted());System.out.println();bubbleSort();printValues();System.out.println("values is sorted: " + isSorted());System.out.println();}}

Database System Concepts
7th Edition
ISBN:9780078022159
Author:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Chapter1: Introduction
Section: Chapter Questions
Problem 1PE
icon
Related questions
Question

Hello, I was hoping you can help me figure out how to make this Bubble Sort go from Ascending to Descending order (instead of 1,2,3 it needs to be 3,2,1)

import java.text.DecimalFormat;
import java.util.Random;

public class Question4 {
static final int SIZE = 50; // size of array to be sorted
static int[] values = new int[SIZE]; // values to be sorted

static void initValues()
// Initializes the values array with random integers from 0 to 99.
{
Random rand = new Random();
for (int index = 0; index < SIZE; index++)
values[index] = Math.abs(rand.nextInt()) % 100;
}

static public boolean isSorted()
// Returns true if the array values are sorted and false otherwise.
{
for (int index = 0; index < (SIZE - 1); index++)
if (values[index] > values[index + 1])
return false;
return true;
}

static public void swap(int index1, int index2)
// Precondition: index1 and index2 are >= 0 and < SIZE.
//
// Swaps the integers at locations index1 and index2 of the values array.
{
int temp = values[index1];
values[index1] = values[index2];
values[index2] = temp;
}

static public void printValues()
// Prints all the values integers.
{
int value;
DecimalFormat fmt = new DecimalFormat("00");
System.out.println("The values array is:");
for (int index = 0; index < SIZE; index++)
{
value = values[index];
if (((index + 1) % 10) == 0)
System.out.println(fmt.format(value));
else
System.out.print(fmt.format(value) + " ");
}
System.out.println();
}
/////////////////////////////////////////////////////////////////
//
// Bubble Sort

static void bubbleUp(int startIndex, int endIndex)
// Switches adjacent pairs that are out of order
// between values[startIndex]..values[endIndex]
// beginning at values[endIndex].
{
for (int index = endIndex; index > startIndex; index--)
if (values[index] < values[index - 1])
swap(index, index - 1);
}



static void bubbleSort()
// Sorts the values array using the bubble sort algorithm.
{
int current = 0;

while (current < (SIZE - 1))
{
bubbleUp(current, SIZE - 1);
current++;
}
}


public static void main(String[] args)
{
initValues();
printValues();
System.out.println("values is sorted: " + isSorted());
System.out.println();

bubbleSort();

printValues();
System.out.println("values is sorted: " + isSorted());
System.out.println();
}
}

Expert Solution
trending now

Trending now

This is a popular solution!

steps

Step by step

Solved in 4 steps with 1 images

Blurred answer
Knowledge Booster
Arrays
Learn more about
Need a deep-dive on the concept behind this application? Look no further. Learn more about this topic, computer-science and related others by exploring similar questions and additional content below.
Similar questions
  • SEE MORE QUESTIONS
Recommended textbooks for you
Database System Concepts
Database System Concepts
Computer Science
ISBN:
9780078022159
Author:
Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:
McGraw-Hill Education
Starting Out with Python (4th Edition)
Starting Out with Python (4th Edition)
Computer Science
ISBN:
9780134444321
Author:
Tony Gaddis
Publisher:
PEARSON
Digital Fundamentals (11th Edition)
Digital Fundamentals (11th Edition)
Computer Science
ISBN:
9780132737968
Author:
Thomas L. Floyd
Publisher:
PEARSON
C How to Program (8th Edition)
C How to Program (8th Edition)
Computer Science
ISBN:
9780133976892
Author:
Paul J. Deitel, Harvey Deitel
Publisher:
PEARSON
Database Systems: Design, Implementation, & Manag…
Database Systems: Design, Implementation, & Manag…
Computer Science
ISBN:
9781337627900
Author:
Carlos Coronel, Steven Morris
Publisher:
Cengage Learning
Programmable Logic Controllers
Programmable Logic Controllers
Computer Science
ISBN:
9780073373843
Author:
Frank D. Petruzella
Publisher:
McGraw-Hill Education