Home Upgrade Search Memberlist Extras Hacker Tools Award Goals Help Wiki Contact

Java 

Need Opinions on how I can improve my Java Code

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.




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();
        }
    }
    
} 
I don't have time to go in depth now, but review your naming. If your variable needs a comment to explain what it stores, you've given it a wrong name. Names should be clear

And sortArray, sortArray2, ... isn't a good idea either; simply appending your name with a number makes it easy to confuse them. And no need to include "array" in the name; it's 2017 and we have proper tools that tell us what type it is.

Loop indices, on the other hand, are an exception to that - calling them i, instead of index, is perfectly fine as that's a convention most programmers follow: anyone who sees a variable named i will assume it stores an index.

This all comes down to one simple principle: use meaningful, intention-revealing names.

Edit: and try not to generate output in methods like selectionSort(): methods should do one thing, and do it well. Printing text is an unexpected and undocumented side effect in cases like these.
(Mar 30, 2017 01:51 PM)Dialog Wrote:  I don't have time to go in depth now, but review your naming. If your variable needs a comment to explain what it stores, you've given it a wrong name. Names should be clear

And sortArray, sortArray2, ... isn't a good idea either; simply appending your name with a number makes it easy to confuse them. And no need to include "array" in the name; it's 2017 and we have proper tools that tell us what type it is.

Loop indices, on the other hand, are an exception to that - calling them i, instead of index, is perfectly fine as that's a convention most programmers follow: anyone who sees a variable named i will assume it stores an index.

This all comes down to one simple principle: use meaningful, intention-revealing names.

Edit: and try not to generate output in methods like selectionSort(): methods should do one thing, and do it well. Printing text is an unexpected and undocumented side effect in cases like these.


I appreciate the feed back. You are absolutely right I should write up better names for my code. Especially since this will be graded and perfection is everything thank you again for your input.


Possibly Related Threads...
Thread / Author Replies Views Last Post
[Java] by RegularJoeAug 4, 2019 02:26 PM 3 318 Aug 4, 2019 02:26 PM
Last Post: Pinhead Larry
[C] by VoxiNov 7, 2018 10:23 PM 3 205 Nov 7, 2018 10:23 PM
Last Post: Souffrance
[Python] by -Matatan-Mar 12, 2018 10:46 AM 8 219 Mar 12, 2018 10:46 AM
Last Post: gulliemam