Question

In: Computer Science

Q: Let’s say you have an unordered list of numbers and you wanted to put them...

Q: Let’s say you have an unordered list of numbers and you wanted to put them in order from lowest to highest value. How would you do that? You’re probably thinking that you would just look at all the numbers, find the lowest number and put it at the beginning of your list. Then you would find the next largest number and put it in the second spot in the list, and so on until you’ve ordered the entire list of numbers. It’s simple, basic, and not very exciting. Now, let’s say that instead of ordering the list yourself, you decide it’s a better idea to write a computer program to order the list for you. Now you don’t have to deal with moving the numbers around, you just need to tell your program how to move the numbers, and then let the program handle any list you give it.Identify all possible ways of telling your program how to move the numbers, where each way provides the required result.

(Note: The program code is preferred to be in Java)

Solutions

Expert Solution

The solution to the above problem is:-

I have sorted the list by selecting the smallest number and then swapping them with the current loop variable index value.

i.e, if the smallest value is in the 3rd index and my current loop value index value is 1 then swapping is used to replace the smallest value. i.e, the 3rd index contains the value of the 1st index.

This is similar to the selection sort, where we will find the smallest numbers and order the list.

code for the above problem is:-

import java.util.*;
import java.lang.*;
class Selection
{
   public static void main(String args[])
   {
       Scanner sc = new Scanner(System.in); // for taking input.
       int n;
       System.out.println("Ente the size of the array: ");
       n=sc.nextInt(); //taking the size of the array from the user.
       int arr[] = new int[n]; // creating an array with the size taken from the user.
       int i;
       System.out.println("Enter the array elements:");
       for(i=0;i<n;i++)
       {
           arr[i]=sc.nextInt(); // creating an array with elements taken from the user.
       }
       System.out.print("Before arraing array elements are: ");
       for(i=0;i<n;i++)
       {
           System.out.print(arr[i]+" "); //printing the elements.
       }
       for(i=0;i<n;i++)
       {
           int index=i; // to find the index value of the minimum number.
           for(int j=i+1;j<n;j++)
           {
               if(arr[j]<arr[index])
               {
                   index=j; // finding the minimum value's index .
               }
           }
           int temp=arr[i]; // swapping the minimum number with the current element in the array.
           arr[i]=arr[index]; // swap arr[min] with arr[i] so that min number will be in the starting of the loop.
           arr[index]=temp;
       }
       System.out.print("\nElements after arranging are: "); // after sorting printing the elements.
       for (i=0;i<n;i++)
       {
           System.out.print(arr[i]+" ");
       }
       System.out.println();
   }
}
  

Screenshots of the code:-

Output for the above code is:-

  


Related Solutions

Let’s say you wanted to have a communications satellite orbit the Moon so that it stayed...
Let’s say you wanted to have a communications satellite orbit the Moon so that it stayed exactly above one point of the Moon’s equator (similar to a geosynchronous here on Earth). What would the linear speed and lunar altitude of your communications satellite be?
So let’s say you have a beaker that contains 6.02g ammonium chloride and to this you...
So let’s say you have a beaker that contains 6.02g ammonium chloride and to this you add 300.0mL of 0.450 M calcium hydroxide according to the following chemical equation: Ca(OH)2(aq) + 2NH4Cl(s)  CaCl2(aq) + 2H2O(l) + 2NH3(g) H = +90.66 kJ A) draw the beaker at the end of the reaction- what would be in the beaker and or around it. what would the products look like on a molecular/atom/ion level? B) at STP, how many L of your...
So let’s say you have a beaker that contains 78.8g of ammonium carbonate and to this...
So let’s say you have a beaker that contains 78.8g of ammonium carbonate and to this you add 175 mL of 5.80 M hydrochloric acid according to the following chemical equation: 2HCl(aq) + (NH4)2CO3(s) -- > 2NH4Cl(aq) + H2O(l) + CO2(g) .DeltaH = -145 kJ A) Draw the beaker at the end of the reaction - what would e in the beaker and/or around it. What would the products look like on a molecular/atom/ion level? B) Why would water vapor...
Let’s say you have the ciphertext for the given plaintext. Plaintext: it was disclosed yesterday that...
Let’s say you have the ciphertext for the given plaintext. Plaintext: it was disclosed yesterday that several informal but direct contacts have been made with political representatives of the viet cong in Moscow Ciphertext: UZQSOVUOHXMOPVGPOZPEVSGZWSZOPFPESXUDBMETSXAIZVUEPHZHMDZSHZOWSFPAPPDTSVPQUZWYMXUZUHSXEPYEPOPDZSZUFPOMBZWPFUPZHMDJUDTMOHMQ Can you determine the cipher used? If yes, name the cipher. Explain why. Can you determine all/part of the key. If yes, give the key or part of the key. Explain how you deduced it.
concatenate(lst1, lst2 ) Given two (possibly empty) unordered lists, concatenate them such that the first list...
concatenate(lst1, lst2 ) Given two (possibly empty) unordered lists, concatenate them such that the first list comes first.Example: When the first input unordered list lst1 is [1, 2, 3] and the second lst2 is [7,8,9], the outputunordered list is [1,2,3,7,8,9]. Use codes: class Node: def __init__(self, data): self.data = data self.next = None def getData(self): return self.data def getNext(self): return self.next def setData(self, data): self.data = data def setNext(self, node): self.next = node class UnorderedList: def __init__(self): self.head = None...
Lets say you wanted to predict success in a college course - that is, you wanted...
Lets say you wanted to predict success in a college course - that is, you wanted to isolate some variables that have a causal influence on good performance in class. What are some potential INDEPENDENT variables that you might manipulate in an experimental design to test their effect on the DEPENDENT variable of course performance?
So let’s say you have a beaker that contains 15.2 g sodium sulfite and to this...
So let’s say you have a beaker that contains 15.2 g sodium sulfite and to this you add 150. mL of 1.25 M nitric acid according to the following chemical equation: Na2SO3(s) + 2HNO3 (aq)  2NaNO3 (aq)+ H2O(l) + SO2 (g)   H = -225. kJ A). Draw the beaker at the end of the reaction. What would e in the beaker and/or around it. What would the products look like on a molecular/atom/ion level? B). At STP, how many...
Let’s say that Major League Baseball has decided to put an expansion franchise in Portland, Oregon....
Let’s say that Major League Baseball has decided to put an expansion franchise in Portland, Oregon. Considering what you now know about branding, what would be a good nickname for that team? Why would that be a good nickname? Explain your choice using branding principles.
Let’s say you have a light shop where you sale and purchase having different types of...
Let’s say you have a light shop where you sale and purchase having different types of light and different electric appliances. Now using principles of OOP I want you want make an OOP program which can cater the aforementioned problem.(show different types of purchase and sales ) C#
Let’s say you have an unfair six-sided die that lands on 2 exactly 20% of the...
Let’s say you have an unfair six-sided die that lands on 2 exactly 20% of the time. If you roll this “loaded” die 5 times, what are the odds that you: (a) never roll a 2, (b) roll a 2 two times, or (c) roll a 2 more than two times? (use excel and show functions)
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT