Question

In: Computer Science

Your code must print all the steps in the output as an Eg> When you run...

Your code must print all the steps in the output as an Eg>

When you run your Merge sort code the sequence of output should be:

1) Enter input sequence

2) Show n/2 division of the input sequence

3) keep showing n/2 division of your sequence until you get one attribute

4) Sort first two numbers

5) Make a stack of 4 by merging 2 * 2 and sort them

6) keep showing merging and sorting until you show the final merging of last two stacks and sort them.

Similarly, show all the steps for Bubble sort.

Mostly need Bubble in Java, please.v

Solutions

Expert Solution

public class BubbleSortExample {  

    static void bubbleSort(int[] arr) {  

        int n = arr.length;  

        int temp = 0;  

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

                 for(int j=1; j < (n-i); j++){  

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

                                 //swap elements  

                                 temp = arr[j-1];  

                                 arr[j-1] = arr[j];  

                                 arr[j] = temp;  

                         }  

                          

                 }  

         }  

  

    }  

    public static void main(String[] args) {  

                int arr[] ={3,60,35,2,45,320,5};  

                 

                System.out.println("Array Before Bubble Sort");  

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

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

                }  

                System.out.println();  

                  

                bubbleSort(arr);//sorting array elements using bubble sort  

                 

                System.out.println("Array After Bubble Sort");  

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

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

                }  

   

        }  

}  


Related Solutions

hi! I have this code. when I run it, it works but in the print(the number...
hi! I have this code. when I run it, it works but in the print(the number we are searching for is ) it prints the number twice. ex. for 5 it prints 55 etc. apart from this, it works #include <stdio.h> #include <stdlib.h> #include <time.h> int main(){ int n,i; printf("Give me the size of the table : \n"); scanf("%d", &n); int A[n],x,y; for (i=0;i<n;i++) A[i]=rand() % n; for (i=0;i<n;i++) printf("%d\n", A[i]); srand(time(NULL)); y=rand()% n ; printf("The number we are searching...
Below is my code in C#, When I run it, the output shows System.32[], Can you...
Below is my code in C#, When I run it, the output shows System.32[], Can you please check and let me know what is the problem in the code. class Program { static void Main(string[] args) { int number=12; Console.WriteLine(FizzArray(number)); } public static int[] FizzArray(int number) { int[] array = new int[number]; for (int i = 1; i < number; i++) array[i] = i; return array; }
List all major steps when developing android applications. Use code examples if possible. These steps should...
List all major steps when developing android applications. Use code examples if possible. These steps should pertain to the most commonly distributed android applications. Only consider Java-based applications.
Print out the following output. You must use exactly 2 for statements, 2 range statements, and...
Print out the following output. You must use exactly 2 for statements, 2 range statements, and 3 print statements. (python) Calculating x ** 1 1 3 5 ==== Calculating x ** 2 1 9 25 ==== Calculating x ** 3 1 27 125 ====
Q. 3 [35 pts] Print out the following output. You must use exactly 2 for statements,...
Q. 3 [35 pts] Print out the following output. You must use exactly 2 for statements, 2 range statements, and 3 print statements. No modules are to be used for this problem. Calculating x ** 1 1 3 5 ==== Calculating x ** 2 1 9 25 ==== Calculating x ** 3 1 27 125 ====
How to print output vertically in the code attached below: import operator with open ('The Boy...
How to print output vertically in the code attached below: import operator with open ('The Boy .txt',encoding='utf8') as my_file: contents=my_file.read() l = contents.strip() words = l.split() newdict = dict() for i in words: if i in newdict: newdict[i] = newdict[i] + 1 else: newdict[i] = 1 newly = dict() sorted_Dict = sorted(newdict.items(), key=operator.itemgetter(1), reverse=True) for n in sorted_Dict: newly[n[0]] = n[1] print(newly) with open('freqs.txt','w', encoding='utf8') as final: final.write(str(newly))
Linux-Practise1: Run the following commands (Print working directory) pwd. What is the output of the above...
Linux-Practise1: Run the following commands (Print working directory) pwd. What is the output of the above command Create a file named Linux1 nano Linux1 Type the following in the file Hello world Save the file Create a copy of the file cp Linux1 Linux2 view the listing of the files in the directory            ls What is the output of the above command rename the file mv Linux2 Linux3 view the listing of the files in the directory            ls...
Must show all needed steps in getting to your final answer. And, express your final answer...
Must show all needed steps in getting to your final answer. And, express your final answer as a decimal. Show every step NEATLY please NEATLY AND VERY CLEAR AND READABLE PLEASE TYPE ANSWER AND WORK NEATLY AND VERY CLEAR AND READABLE PLEASE TYPE ANSWER AND WORK Part 1) Suppose that you are planning to travel a certain air route via plane once each week for your new job. Also, assume that there is a 3% chance that your outbound flight...
You run a small community or country and your primary output is the growing of wheat...
You run a small community or country and your primary output is the growing of wheat and the sewing of socks. You are able to produce both products for a low cost so your residents do not have to pay very much for these two products. One day, another country contacts you with the offer of the same products at an even lower price. If you allow them entry into your community, your residents will save money. However, those who...
(Python) This is my code for printing a roster for a team. When I print to...
(Python) This is my code for printing a roster for a team. When I print to the console, it makes the first player's name show up as number 2, and it says [] (its just blank for 1). How can I fix that so the first player's name is 1, not skipping 1 and going to 2. def file_to_dictionary(rosterFile): myDictionary={} myDict=[]    with open(rosterFile,'r') as f: for line in f:    (num,first,last,position)=line.split() myDictionary[num]= myDict myDict=[first, last, position] print (myDictionary) return...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT