Question

In: Computer Science

What would the code look like if you weren't using hash maps and array lists (only...

What would the code look like if you weren't using hash maps and array lists (only using scanner import) and solving using a partition algorithm?

Solutions

Expert Solution

partition algorithm:

import java.util.Scanner;

public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int f = sc.nextInt();
int[] ar = new int[f];
for (int i = 0; i < f; i++) {
ar[i] = sc.nextInt();
}
sc.close();
partition(ar);
printAr(ar);
}
  
  
public static int partition(int [] ar) {
int pivotIndex = 0;
int pivotValue = ar[pivotIndex];
  
swap(ar, pivotIndex, ar.length - 1);
  
  
int indexToReturn = 0;
for (int i = 0; i < ar.len; i++){
if (ar[i] < pivotValue){
swap(ar, i, indexToReturn);
indexToReturn++;
}
}
  
swap(ar, indexToReturn, ar.length - 1);
return indexToReturn;
}
  
private static void swap(int [] ar, int i, int j) {
int temp = ar[i];
ar[i] = ar[j];
ar[j] = temp;
}
  
private static void printAr(int[] ar) {
for (int num: ar) {
System.out.print(num + " ");
}
System.out.println();
}
}

this is how an  partition algorithm looks like without using hash maps and array lists.


Related Solutions

I actually would like you to take a look at my code and point it out...
I actually would like you to take a look at my code and point it out if there is anything wrong with it. I think there might be because there are some values that are the same in different arrays. QUESTION: C++ Use the random number generator in class Random to store a list of 1000 random integer values in an array. Create 3 arrays using this method. Apply each of the insertion, bubble, selection and shell sort algorithms to...
What would an example of java code look like? Producer class - Declaring toolsPerHour as an...
What would an example of java code look like? Producer class - Declaring toolsPerHour as an instance variable - Creating constructor that takes toolsPerHour - Creating getter for toolsPerHour - Method for calculating days produced Tester class - Creating 3 producer objects using constructor - Setting toolsPerHour - Calling methods - Displaying output
Using MongoDB, what command would you enter to retrieve this document using only the zip code...
Using MongoDB, what command would you enter to retrieve this document using only the zip code 11242? db.inspections.find(???????) Database: city Collection: inspections {"_id":{"$oid":"56d61033a378eccde8a898ae"}, "id":"23536-2015-ENFO", "certificate_number":5373970, "business_name":"NZO CORP.", "date":"Apr 22 2015", "result":"Violation Issued", "sector":"Grocery-Retail - 808", "address":{"city":"BROOKLYN", "zip":11242, "street":"COURT ST", "number":26}}
Java Hash table, What does the separate chaining ST look like for M=3 and the following...
Java Hash table, What does the separate chaining ST look like for M=3 and the following input key, hash pairs? A,0 F,0 Z,2
what would a morning and evening gratitude practice look like to you? and how would it...
what would a morning and evening gratitude practice look like to you? and how would it improve your sleep hygiene, stress management, and overall outlook on life?
1. What would the regression output (analysis) look like using this multiple regression equation and the...
1. What would the regression output (analysis) look like using this multiple regression equation and the following data? Daily Gross Revenue= total daily income+b1*daily tour income+b2*number of tourists+b3*Friday+b4*Saturday 2. What's the multiple regression equation with the numbers from the output? Years Weekend Daily Tour Income Number of Tourists Daily Gross Revenue Total Daily Income 1 Friday 3378 432 4838.95 8216.95 1 Saturday 1198 139 3487.78 4685.78 1 Sunday 3630 467 4371.3 8001.3 2 Friday 4550 546 6486.48 11036.48 2 Saturday...
If you are not currently employed, look at a company you would like to work for...
If you are not currently employed, look at a company you would like to work for and look at their retirement program. Next, what concept do you find most challenging about retirement?
What would a more democratic United States look like?
What would a more democratic United States look like?
If you are performing an analysis of a company, would you only look at the last...
If you are performing an analysis of a company, would you only look at the last year's performance ? Please explain your answer. Yes or no is not sufficient.
1. Explain what you would assume the yield curve would look like during economic expansion and...
1. Explain what you would assume the yield curve would look like during economic expansion and why. 2. Why are investors and managers concerned about stock market efficiency?
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT