Mar 30, 2017 01:07 PM
(This post was last modified: Mar 30, 2017 01:09 PM by Rin™.)
I am currently looking on any advice on my code.
Is there anywhere I can improve or even if there is an easier way to do this.
Any input really would be great on my code thank you.
Is there anywhere I can improve or even if there is an easier way to do this.
Any input really would be great on my code thank you.
PHP Code
<?php
public static void main(String[] args)
{
int [] sortArray = {3, 9, 6, 1, 2, 5}; //original array to be sorted
int [] sortArray2 = {3, 9, 6, 1, 2, 5}; //duplicate array to be sorted
int [] sortArray3 = {3, 9, 6, 1, 2, 5}; //duplicate array to be sorted
//print contents of unsorted original array
System.out.println("-------Unsorted Array-------");
for (int index=0; index<sortArray.length; index++)
{
System.out.print(sortArray[index] + "\t");
}
System.out.println("\n");
//run selection sort method and print sorted array
selectionSort(sortArray);
System.out.println("----Sorted Array with selection sort----");
for (int index=0; index<sortArray.length; index++)
{
System.out.print(sortArray[index] + "\t");
}
System.out.println();
//-----------------------------------------------------------------
System.out.println("\n\n***************************************************");
//print contents of unsorted original array(#2)
System.out.println("\n\n-------Unsorted Array2-------");
for (int index=0; index<sortArray2.length; index++)
{
System.out.print(sortArray2[index] + "\t");
}
System.out.println("\n");
//run insertion sort method and print sorted array
insertionSort(sortArray2);
System.out.println("----Sorted Array2----");
for (int index=0; index<sortArray2.length; index++)
{
System.out.print(sortArray2[index] + "\t");
}
System.out.println();
//-----------------------------------------------------------------
System.out.println("\n\n***************************************************");
//print contents of unsorted original array(#3)
System.out.println("\n\n-------Unsorted Array3-------");
for (int index=0; index<sortArray3.length; index++)
{
System.out.print(sortArray3[index] + "\t");
}
System.out.println("\n");
//run insertion sort method and print sorted array
bubbleSort(sortArray3);
System.out.println("----Sorted Array3----");
for (int index=0; index<sortArray3.length; index++)
{
System.out.print(sortArray3[index] + "\t");
}
System.out.println();
}
//-----------------------------------------------
//selection sort method
//-----------------------------------------------
public static void selectionSort(int [] array)
{
int minimum; //keeps track of the index of the smallest item in array
int temp; //temp value to use in swap
int count=0; //counter for number of passes
System.out.println("-----Running selection sort-----"); //header
for(int index = 0; index < array.length-1; index++)
{
minimum = index; //assign first item being reviewed as minimum
for (int nextIndex=index+1; nextIndex < array.length; nextIndex++)
{
if (array[nextIndex] > array[minimum]) //reverses the order
{
minimum = nextIndex;
}
}
if (minimum != index) //only swap if we have to do so
{
temp = array[index];
array[index] = array[minimum];
array[minimum] = temp;
}
//block of code to print array contents after each pass
count++; //counts passes through method
//prints out contents of array after each pass
System.out.print("After " + count + " pass(es), the array looks like this: ");
for(int loop = 0; loop < array.length; loop++)
{
System.out.print("\t" + array[loop]);
}
System.out.println();
}
System.out.println(); //for spacing
}
//-----------------------------------------------
//insertion sort method
//-----------------------------------------------
public static void insertionSort(int [] array2)
{
int place;
int toSort; //value to be sorted
int count = 0;
System.out.println("-----Running insertion sort-----"); //header
//keeps track of current place in the unsorted portion of the array
for (int index = 1; index < array2.length; index++)
{
place = index; //place being reviewed
toSort = array2[index]; //value to be sorted
//iterates through sorted portion of the list and checks to see if the current
//position is less than what is in the next position of the sorted list
while (place > 0 && (array2[place - 1] < toSort)) //reverse the order
{
array2[place] = array2[place - 1];
place--;
}
array2[place] = toSort;
//block of code to print array contents after each pass
count++; //counts passes through method
//prints out contents of array after each pass
System.out.print("After " + count + " pass(es), the array looks like this: ");
for(int loop = 0; loop < array2.length; loop++)
{
System.out.print("\t" + array2[loop]);
}
System.out.println();
}
System.out.println(); //spacing
}
//Bubble sort
public static void bubbleSort(int [] array3)
{
int temp; //temp value to use in swap
int count = 0;
System.out.println("-----Running bubble sort-----"); //header
for(int index=0; index < array3.length; index++) //work through array from beginning
{
for(int nextIndex=1; nextIndex < (array3.length-index); nextIndex++) //compare to next
{
//if the next one is smaller than the one preceding it, swap
if(array3[nextIndex-1] < array3[nextIndex]) //reversed the order using a less then sign
{
//swap elements
temp = array3[nextIndex-1];
array3[nextIndex-1] = array3[nextIndex];
array3[nextIndex] = temp;
}
}
//block of code to print array contents after each pass
count++; //counts passes through method
//prints out contents of array after each pass
System.out.print("After " + count + " pass(es), the array looks like this: ");
for(int loop = 0; loop < array3.length; loop++)
{
System.out.print("\t" + array3[loop]);
}
System.out.println();
}
}
}


