Dana Vrajitoru
C101/I210 Computer Programming

C101/I210 Lab 12

Date: Wednesday, April 22, 2020.
Due date: Wednesday, April 29, 2020.

Objective: To work with sorting functions for arrays in Java.

Ex. 1. Sorting Arrays Methods

In this project we will write a few methods manipulating arrays.

Create a Java project in Eclipse and call it lab12. Create a class in this project called ArraySort and containing the main method. Write your name, course, semester, and homework number at the top as a comment.

Add the code for the definition of a scanner, but make it a static class attribute instead of a variable in the main. The slides contain an example of how to do this.

a. Input and Output

Copy the methods from the last lab to input and output an array.

static void printArray(int[] a) {
    for (int i = 0; i < a.length; i++)
        System.out.println(" " + a[i]);
    System.out.println();
}
static int[] inputArray() {
    int[] a;
    System.out.println("Enter the size");
    int size = scan.nextInt();
    a = new int[size];
    System.out.println("Enter the elements of the array");
    for (int i = 0; i < size; i++)
        a[i] = scan.nextInt();
    return a;
}

b. Bubble Sort and Selection Sort

Copy the methods bubbleSort and selectionSort discussed in the lecture, as well as their helping methods.

static void swap(int[] a, int i, int j) {
    int temp = a[i];
    a[i] = a[j];
    a[j] = temp;
}

static void bubbleSort(int[] a) {
    int n = a.length;
    boolean sorted = false;
    while (n > 1 && !sorted) {
        sorted = true;
        for (int i = 0; i < n - 1; i++)
            if (a[i] > a[i + 1]) {
                swap(a, i, i + 1);
                sorted = false;
            }
        n--;
    }
}
static int maxIndex(int[] a, int end) {
    int i, max = 0;
    for (i = 1; i <= end; i++)
        if (a[i] > a[max])
            max = i;
    return max;
}
static void selectionSort(int[] a) {
    int max;
    for (int i = a.length - 1; i > 0; i--) {
        max = maxIndex(a, i);
        if (max != i)
            swap(a, max, i);
    }
}

Test each of these functions in the main. Declare an array of integers and input it from the user like the last time, then call the bubble sort and output the array. Then change the call to the selection sort to make sure both of these methods work.

c. Closest Pair

Let's write a method that takes in a sorted array and find the two closest values in that array. For that, go through the array and take the absolute value of the difference between adjacent elements, then find the minimum along the array. The method should output the two closest values in the array and not return anything. The header of the function should be

static void closestPair(int[] a)

Use the method Math.abs to find the absolute value of the difference between the numbers. Initialize a variable difference with the absolute value of the difference between a[0] and a[1], an minIndex with the value 0. Use a for loop with i going form 1 to the length-2. For every i, if the absolute value of the difference between a[i] and a[i+1] is less than the current value of the difference, update the value of the difference and minIndex. At the end, output a[minIndex] and a[minIndex+1] as the closest pair in the array.

Add a test for this function in the main.

Run the program to make sure everything works correctly.

Turn in:

Upload ArraySort.java to Canvas in Assignments - Homework 12, or wait until you have completed the homework to upload both files at the same time.