0% found this document useful (0 votes)
2 views

Coding activity 1

The document provides instructions for a coding activity where a method named sortAndPrintReverse is to be implemented to sort an array of strings in descending order using insertion sort. The method must print the array after each insertion, showing the current state of the array. Additionally, a runner class is included to test the method without a main method in the U7_L6_Activity_One class.

Uploaded by

roopsaini93
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

Coding activity 1

The document provides instructions for a coding activity where a method named sortAndPrintReverse is to be implemented to sort an array of strings in descending order using insertion sort. The method must print the array after each insertion, showing the current state of the array. Additionally, a runner class is included to test the method without a main method in the U7_L6_Activity_One class.

Uploaded by

roopsaini93
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 3

Unit 7: Lesson 6 - Coding Activity 1

Instructions

Write a method, public static void sortAndPrintReverse(String [] arr), which implements an


insertion sort on the array arr. The Strings should be sorted in descending order.

Unlike in the exercise from the previous lesson, this method should print the values in the
array (on one line with a single space between values) multiple times: once after each
insertion in the algorithm has been completed (even if a value is inserted in its original
place). Make sure to use the String.compareTo() method when sorting to decide the correct
order of the Strings.

For example, if sortAndPrintReverse method is called with the array arr initialized as {"lock",
"key", "nail", "anvil", "hammer"} then the following should be printed:

lock key nail anvil hammer


nail lock key anvil hammer
nail lock key anvil hammer
nail lock key hammer anvil

Write your sortAndPrintReverse method in the U7_L6_Activity_One class. Use the runner
class to test your method but do not add a main method to your U7_L6_Activity_One.java
file or your code will not be scored correctly.

runner_U7_L6_Activity_One

import java.util.Scanner;

public class runner_U7_L6_Activity_One

public static void main(String[] args)

Scanner scan = new Scanner(System.in);

System.out.println("Enter array length:");

int len = scan.nextInt();

scan.nextLine();
String[] wordList = new String[len];

System.out.println("Enter values:");

for(int i = 0; i < len; i++)

wordList[i] = scan.nextLine();

U7_L6_Activity_One.sortAndPrintReverse(wordList);

U7_L6_Activity_One

public class U7_L6_Activity_One {

public static void sortAndPrintReverse(String[] arr) {

int n = arr.length;

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

// Get the next value to be inserted into the partially sorted array

String key = arr[i];

int j;

// If there is a value to the left, check to see if our value should be placed before it

for (j = i - 1; j >= 0 && arr[j].compareTo(key) < 0; j--) {

// Move that value to the right

arr[j + 1] = arr[j];

}
// When it no longer is less than the value to the left, insert the value

arr[j + 1] = key;

// Print the array after each insertion

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

System.out.print(arr[k]);

if (k < arr.length - 1) {

System.out.print(" ");

System.out.println();

You might also like