Question

Home Work No.2 due 09.11.2019 either ascending or descending SORTING: An array is said to be ordered if its values order. In

Programming language: Java

0 0
Add a comment Improve this question Transcribed image text
Answer #1

ANSWER: Here I am giving you the code and output if you have any doubt you can comment or like my solution.

NOTE: Here no need of SortList() method this problem can be solved by using only two methods and there is no description is given for each method also.

CODE:


public class SortAnArray {

   void BubbleSort(int array[])
{
int n = array.length;
for (int i = 0; i < n-1; i++)
for (int j = 0; j < n-i-1; j++)
if (array[j] > array[j+1])
{

int temp = array[j];// swapping the value of the array when condition becomes true.
array[j] = array[j+1];
array[j+1] = temp;
}
}

  
void PrintList(int array[])
{
int n = array.length;
for (int i=0; i<n; ++i)
System.out.print(array[i] + " ");
System.out.println();
}

public static void main(String args[])
{
   SortAnArray ob = new SortAnArray();
  
int array[] = {84, 24, 15, 92, 32, 18, 98};// created an array which is not sorted.
  
  
System.out.println("Array before bubble sort:");
ob.PrintList(array);
ob.BubbleSort(array);
System.out.println("Array After bubble sort:");
ob.PrintList(array);
}

}

OUTPUT:

Array before bubble sort: 84 24 15 92 32 18 98 Array After bubble sort: 15 18 24 32 84 92 98

Add a comment
Know the answer?
Add Answer to:
Programming language: Java Home Work No.2 due 09.11.2019 either ascending or descending SORTING: An array is...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Java Programming Write a program to find the number of comparison using binarySearch and the sequentialSearch...

    Java Programming Write a program to find the number of comparison using binarySearch and the sequentialSearch algorithms as follows: Suppose list is an array of 2500 elements. 1. Use a random number generator to fill list; 2. Use a sorting algorithm to sort list; 3. Search list for some items as follows: a) Use the binary search algorithm to search list (please work on SearchSortAlgorithms.java and modify the algorithm to count the number of comparisons) b) Use the sequential search...

  • //Generic interface that describes various searching and sorting //algorithms. Note that the type parameter is unbounded....

    //Generic interface that describes various searching and sorting //algorithms. Note that the type parameter is unbounded. However, //for these algorithms to work correctly, the data objects must //be compared using the method compareTo and equals. //In other words, the classes implementing the list objects //must implement the interface Comparable. The type parameter T //is unbounded because we would like to use these algorithms to //work on an array of objects as well as on objects of the classes //UnorderedArrayList and...

  • Another simple sort is the odd-even sort. The idea is to repeatedly make two passes through the a...

    Another simple sort is the odd-even sort. The idea is to repeatedly make two passes through the array. On the first pass you look at all the pairs of items, a[j] and a[j+1], where j is odd (j = 1, 3, 5, …). If their key values are out of order, you swap them. On the second pass you do the same for all the even values (j = 2, 4, 6, …). You do these two passes repeatedly until...

  • 11:10 PM No SIM mycourselink.lakeheadu.ca Modify the following bubble sort program to improve its performance I...

    11:10 PM No SIM mycourselink.lakeheadu.ca Modify the following bubble sort program to improve its performance I Fig. 6.15: fig06 15.c 2 Sorting an array's values into ascending order. Finclude <stdio.h> 4 #define SIZE 10 6 function main begins program execution 7 int main(void) 9 initialize a lo int a CSIZEJ 12, 6, 4, 8, 10, 12, 89, 68, 45, 37) 12 putsc Data items in original order output original array IS for (size t i 0; i SIZE ++i) a[i])...

  • Write in C++ (Bubble Sort) implement the bubble sort algorithm - another simple yet inefficient s...

    Write in C++ (Bubble Sort) implement the bubble sort algorithm - another simple yet inefficient sorting technique. its called bubble sort or sinking sort because smaller values gradually "bubble" their way to the top of the array like air bubbles rising in water, while the larger values sink to the bottom of the array. the technique uses nested loops to make several passes through the array. each pass compares successive pairs of elements. if a pair is in increasing order,...

  • Assume that you are sorting an array of 8 elements with quick sort. You just finished...

    Assume that you are sorting an array of 8 elements with quick sort. You just finished the first pass and the array looks like below. Which statement is true for the pivot value? 4 8 12 16 18 20 22 24 QUICKSORT ALGORITHM Quicksort selects a specific value called a pivot and rearranges the array into two parts (called partioning). If the array is randomly ordered, it does not matter which element is the pivot. For simplicity, the first element...

  • Hello I need help with this program. Should programmed in C! Program 2: Sorting with Pointers...

    Hello I need help with this program. Should programmed in C! Program 2: Sorting with Pointers Sometimes we're given an array of data that we need to be able to view in sorted order while leaving the original order unchanged. In such cases we could sort the data set, but then we would lose the information contained in the original order. We need a better solution. One solution might be to create a duplicate of the data set, perhaps make...

  • _______________________________________________________________________________________________ java language-trace instructions". (20 points) Show the contents of the array below, once the contents...

    _______________________________________________________________________________________________ java language-trace instructions". (20 points) Show the contents of the array below, once the contents of the array below, once the "pivot" element is placed at its location after each call of the "Partition” algorithm, in the process of running Quick-Sort on said array. Arrange the data in ascending order (Trom Arrange the data in ascending order (from smallest to largest value). Always select the first element of the partition as "pivot" in data cat B. Apply sorting on...

  • Write a program in MIPS assembly language that implements the DESCENDING bubble sort algorithm to sort a variable-sized array of signed 32-bit integers

    Write a program in MIPS assembly language that implements the DESCENDING bubble sort algorithm to sort a variable-sized array of signed 32-bit integers (words)that are read from the console. Be reminded that in a descending sort, the integers are sorted from the largest to the smallest. A “special value” 99999 will beused to signify the end of the input sequence. This value is not to be considered part of the input data set. However, any value greater than 99999 that...

  • Write a Java program with a single-dimension array that holds 11 integer numbers and sort the...

    Write a Java program with a single-dimension array that holds 11 integer numbers and sort the array using a bubble sort. Next, identify the median value of the 11 integers. Here are the steps your program must accomplish. algorithm (either flowchart or pseudocode) that you will use to write the program Step 1. Create an Place the algorithm in a Word document. 6. Ste the Step 2. Code the program in Eclipse and ensure the following steps are accomplished. 1....

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT