Object-Oriented Programming with Java, part I – Exercise 104

This exercise creates a sorting program that returns the value, swaps values and also returns the index of the smallest value.

My Code

Main

import java.util.Arrays;

public class Main {

    public static void main(String[] args) {
        // write testcode here
        int[] values = {8, 3, 7, 9, 1, 2, 4};
        sort(values);
    }

    public static int smallest(int[] array) {
        // write the code here
        int value = array[0];
        for (int i : array) {
            if (value > i) {
                value = i;
            }
        }
        return value;
    }

    public static int indexOfTheSmallest(int[] array) {
        // code goes here
        int value = array[0];
        int cell = 0;
        int c = 0;
        for (int i : array) {
            if (value > i) {
                value = i;
                cell = c;
            }
            c++;
        }
        return cell;

    }

    public static int indexOfTheSmallestStartingFrom(int[] array, int index) {
        // write the code here
        int value = array[index];

        for (int i = index; i < array.length; i++) {
            if (value > array[i]) {
                value = array[i];
                index = i;
            }
        }

        return index;
    }

    public static void swap(int[] array, int index1, int index2) {
        // code goes here
        int temp = array[index1];
        array[index1] = array[index2];
        array[index2] = temp;
    }

    public static void sort(int[] array) {
        int i = 0;
        while (i < array.length) {
            System.out.println(Arrays.toString(array));
            int index = indexOfTheSmallestStartingFrom(array, i);
            swap(array, i, index);
            i++;
        }
    }
}

Model Code

public class Main {

    public static void main(String[] args) {

        // write testcode here

    }

 

    public static int smallest(int[] table) {

        int smallest = table[0];

        for (int i = 1; i < table.length; i++) {

            if (table[i] < smallest) {

              smallest = table[i];

            }

        }

        return smallest;

    }

 

    public static int indexOfTheSmallest(int[] table) {

        int theIndexOfSmallest = 0;

 

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

            if ( table[i]<table[theIndexOfSmallest]) {

                theIndexOfSmallest = i;

            }

        }

 

        return theIndexOfSmallest;

    }

 

    public static int indexOfTheSmallestStartingFrom(int[] table, int startIndex) {

        int theIndexOfSmallest = startIndex;

 

        for (int i = startIndex; i < table.length; i++) {

            if ( table[i]<table[theIndexOfSmallest]) {

                theIndexOfSmallest = i;

            }

        }

 

        return theIndexOfSmallest;

    }

 

    public static void swap(int[] table, int index1, int index2) {

        int index1OldValue = table[index1];

        table[index1] = table[index2];

        table[index2] = index1OldValue;

    }

 

    public static void sort(int[] table){

        for (int i = 0; i < table.length-1; i++) {

             int theIndexOfTheSmallest = indexOfTheSmallestStartingFrom(table, i);

             swap(table, i, theIndexOfTheSmallest);

        }

    }

}

Comments

At first I thought it was a lot worse than it was. The model codes variables are better, the code cleaner but in most cases i go about things in the same manner. I do appear to have a tendency to try and reinvent the wheel every time I do a different method and this means I end up with more variables than needed.

Leave a comment