Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.java.programmer > #29605 > unrolled thread

Help with java insertion sort program

Started byScott Dunning <scottwd01@gmail.com>
First post2014-08-09 19:11 -0700
Last post2014-08-14 14:30 +0300
Articles 17 — 7 participants

Back to article view | Back to comp.lang.java.programmer


Contents

  Help with java insertion sort program Scott Dunning <scottwd01@gmail.com> - 2014-08-09 19:11 -0700
    Re: Help with java insertion sort program Jeff Higgins <jeff@invalid.invalid> - 2014-08-09 22:28 -0400
      Re: Help with java insertion sort program Jeff Higgins <jeff@invalid.invalid> - 2014-08-09 23:34 -0400
        Re: Help with java insertion sort program Scott Dunning <scottwd01@gmail.com> - 2014-08-09 21:11 -0700
          Re: Help with java insertion sort program Jeff Higgins <jeff@invalid.invalid> - 2014-08-10 00:30 -0400
            Re: Help with java insertion sort program Jeff Higgins <jeff@invalid.invalid> - 2014-08-10 00:45 -0400
              Re: Help with java insertion sort program Jeff Higgins <jeff@invalid.invalid> - 2014-08-10 05:58 -0400
            Re: Help with java insertion sort program Jeff Higgins <jeff@invalid.invalid> - 2014-08-10 05:54 -0400
    Re: Help with java insertion sort program Mike Amling <mamling@chaff.us> - 2014-08-10 06:09 -0500
      Re: Help with java insertion sort program Eric Sosman <esosman@comcast-dot-net.invalid> - 2014-08-10 07:43 -0400
        Re: Help with java insertion sort program Mike Amling <mamling@chaff.us> - 2014-08-13 23:51 -0500
          Re: Help with java insertion sort program Eric Sosman <esosman@comcast-dot-net.invalid> - 2014-08-14 08:07 -0400
        Re: Help with java insertion sort program Mike Amling <mamling@chaff.us> - 2014-08-14 00:46 -0500
      Re: Help with java insertion sort program Arne Vajhøj <arne@vajhoej.dk> - 2014-08-19 16:50 -0400
    Re: Help with java insertion sort program Roedy Green <see_website@mindprod.com.invalid> - 2014-08-12 03:00 -0700
      Re: Help with java insertion sort program Mike Amling <mamling@chaff.us> - 2014-08-13 23:51 -0500
        Re: Help with java insertion sort program Jukka Lahtinen <jtfjdehf@hotmail.com.invalid> - 2014-08-14 14:30 +0300

#29605 — Help with java insertion sort program

FromScott Dunning <scottwd01@gmail.com>
Date2014-08-09 19:11 -0700
SubjectHelp with java insertion sort program
Message-ID<ls6k6e$v0r$1@dont-email.me>
Hello I trying write a sorting program that will ask a user what type of 
sorting method to use (insertion, bubble, selection) and then ask them 
to enter integers to sort.

I thought I had everything correct but apparantly not.  Haha.  This is 
the output and I am not sure where I am going wrong.

Before Sorting:
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]

After Sorting:
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]


Do I seem to be putting things in the correct spot?

Thanks for any help!!

DriverSort class...

     import java.util.Scanner;

     public class DriverSort {

	public static void main(String[] args) {
		
		Scanner scan =new Scanner(System.in);
		Sorter sorter = new Sorter();

	    int choice;  // variable which says which sorting algorithm to use
         		
		System.out.println("1-Insertion sort\n"
						+"2-Selection sort\n"
						+ "3-Bubble sort\n"
						+ "0-quit\n");
		choice = scan.nextInt();
		
		System.out.println("Please enter the number for a sorting method or 
enter 0 to quit: ");
		int size = scan.nextInt();
		int input[] = new int[size];
		
		
	    System.out.println("\nBefore Sorting: ");
	    sorter.printArray(input);
             // sort the array
             Sorter.insertionSort(input);
             System.out.println("\nAfter Sorting: ");
             sorter.printArray(input);
		
		switch (choice) {
		
		case 0:
			System.out.println("Goodbye!");
			System.exit(0);
			break;
			
		case 1: Sorter.insertionSort(input);
         	sorter.printArray(input);
         	break;

         }
       }
    }

Sorter class....


     public class Sorter {
	
	public static int[] insertionSort(int[] input) {
		
		for (int i = 1; i < input.length; i++) {
             int valueToSort = input[i];
             int j = i;
             while (j > 0 && input[j - 1] > valueToSort) {
                 input[j] = input[j - 1];
                 j--;
             }//end while loop.
             // insert the element
             input[j] = valueToSort;
		}//end for loop
             return input;
	}//end insertionSort		

					
		public void printArray(int[] input) {
		    System.out.println(Arrays.toString(input));
			
		}
	}	

	

[toc] | [next] | [standalone]


#29608

FromJeff Higgins <jeff@invalid.invalid>
Date2014-08-09 22:28 -0400
Message-ID<ls6lcd$5nm$1@dont-email.me>
In reply to#29605
On 08/09/2014 10:11 PM, Scott Dunning wrote:
> Hello I trying write a sorting program that will ask a user what type of
> sorting method to use (insertion, bubble, selection) and then ask them
> to enter integers to sort.

I guessing that since you are asking for help here that working
the problem out from first principles on your own is not a requirement.

I'll suggest then that Google is your friend.
Algorithms 4th ed. Sedgewick Wayne
2.1   Elementary Sorts
<http://algs4.cs.princeton.edu/21elementary/>


> I thought I had everything correct but apparantly not.  Haha.  This is
> the output and I am not sure where I am going wrong.
>
> Before Sorting:
> [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
>
> After Sorting:
> [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
> [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
>
>
> Do I seem to be putting things in the correct spot?
>
> Thanks for any help!!
>
> DriverSort class...
>
>      import java.util.Scanner;
>
>      public class DriverSort {
>
>      public static void main(String[] args) {
>
>          Scanner scan =new Scanner(System.in);
>          Sorter sorter = new Sorter();
>
>          int choice;  // variable which says which sorting algorithm to use
>
>          System.out.println("1-Insertion sort\n"
>                          +"2-Selection sort\n"
>                          + "3-Bubble sort\n"
>                          + "0-quit\n");
>          choice = scan.nextInt();
>
>          System.out.println("Please enter the number for a sorting
> method or enter 0 to quit: ");
>          int size = scan.nextInt();
>          int input[] = new int[size];
>
>
>          System.out.println("\nBefore Sorting: ");
>          sorter.printArray(input);
>              // sort the array
>              Sorter.insertionSort(input);
>              System.out.println("\nAfter Sorting: ");
>              sorter.printArray(input);
>
>          switch (choice) {
>
>          case 0:
>              System.out.println("Goodbye!");
>              System.exit(0);
>              break;
>
>          case 1: Sorter.insertionSort(input);
>              sorter.printArray(input);
>              break;
>
>          }
>        }
>     }
>
> Sorter class....
>
>
>      public class Sorter {
>
>      public static int[] insertionSort(int[] input) {
>
>          for (int i = 1; i < input.length; i++) {
>              int valueToSort = input[i];
>              int j = i;
>              while (j > 0 && input[j - 1] > valueToSort) {
>                  input[j] = input[j - 1];
>                  j--;
>              }//end while loop.
>              // insert the element
>              input[j] = valueToSort;
>          }//end for loop
>              return input;
>      }//end insertionSort
>
>
>          public void printArray(int[] input) {
>              System.out.println(Arrays.toString(input));
>
>          }
>      }
>
>
>

[toc] | [prev] | [next] | [standalone]


#29609

FromJeff Higgins <jeff@invalid.invalid>
Date2014-08-09 23:34 -0400
Message-ID<ls6p9j$v7a$1@dont-email.me>
In reply to#29608
On 08/09/2014 10:28 PM, Jeff Higgins wrote:
> On 08/09/2014 10:11 PM, Scott Dunning wrote:
>> Hello I trying write a sorting program that will ask a user what type of
>> sorting method to use (insertion, bubble, selection) and then ask them
>> to enter integers to sort.
>
> I guessing that since you are asking for help here that working
> the problem out from first principles on your own is not a requirement.
>
> I'll suggest then that Google is your friend.
> Algorithms 4th ed. Sedgewick Wayne
> 2.1   Elementary Sorts
> <http://algs4.cs.princeton.edu/21elementary/>

Oops. Skip that since you've already done that homework.
Instead, Google java.io.Console.

Change public void printArray(int[] input) {
to public static void printArray(int[] input) {

and try:

public class DriverSort {
   public static void main(String[] args) {
     int input[] = new int[] { 5, 7, 6, 3, 1 };
     Sorter.insertionSort(input);
     Sorter.printArray(input);
   }
}

>> I thought I had everything correct but apparantly not.  Haha.  This is
>> the output and I am not sure where I am going wrong.
>>
>> Before Sorting:
>> [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
>>
>> After Sorting:
>> [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
>> [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
>>
>>
>> Do I seem to be putting things in the correct spot?
>>
>> Thanks for any help!!
>>
>> DriverSort class...
>>
>>      import java.util.Scanner;
>>
>>      public class DriverSort {
>>
>>      public static void main(String[] args) {
>>
>>          Scanner scan =new Scanner(System.in);
>>          Sorter sorter = new Sorter();
>>
>>          int choice;  // variable which says which sorting algorithm
>> to use
>>
>>          System.out.println("1-Insertion sort\n"
>>                          +"2-Selection sort\n"
>>                          + "3-Bubble sort\n"
>>                          + "0-quit\n");
>>          choice = scan.nextInt();
>>
>>          System.out.println("Please enter the number for a sorting
>> method or enter 0 to quit: ");
>>          int size = scan.nextInt();
>>          int input[] = new int[size];
>>
>>
>>          System.out.println("\nBefore Sorting: ");
>>          sorter.printArray(input);
>>              // sort the array
>>              Sorter.insertionSort(input);
>>              System.out.println("\nAfter Sorting: ");
>>              sorter.printArray(input);
>>
>>          switch (choice) {
>>
>>          case 0:
>>              System.out.println("Goodbye!");
>>              System.exit(0);
>>              break;
>>
>>          case 1: Sorter.insertionSort(input);
>>              sorter.printArray(input);
>>              break;
>>
>>          }
>>        }
>>     }
>>
>> Sorter class....
>>
>>
>>      public class Sorter {
>>
>>      public static int[] insertionSort(int[] input) {
>>
>>          for (int i = 1; i < input.length; i++) {
>>              int valueToSort = input[i];
>>              int j = i;
>>              while (j > 0 && input[j - 1] > valueToSort) {
>>                  input[j] = input[j - 1];
>>                  j--;
>>              }//end while loop.
>>              // insert the element
>>              input[j] = valueToSort;
>>          }//end for loop
>>              return input;
>>      }//end insertionSort
>>
>>
>>          public void printArray(int[] input) {
>>              System.out.println(Arrays.toString(input));
>>
>>          }
>>      }
>>
>>
>>
>

[toc] | [prev] | [next] | [standalone]


#29610

FromScott Dunning <scottwd01@gmail.com>
Date2014-08-09 21:11 -0700
Message-ID<ls6r8g$694$1@dont-email.me>
In reply to#29609
Ok so I realized that I was not filling the array with anything but now 
I'm having issues filling it correctly.  It's only filling the array 
with three of the user inputs and I just cannot figure out why....


import java.util.Scanner;

public class DriverSort {

	public static void main(String[] args) {
		
		Scanner scan =new Scanner(System.in);
		Sorter sorter = new Sorter();

	    int choice;  // variable which says which sorting algorithm to use

		System.out.println("1-Insertion sort\n"
						+"2-Selection sort\n"
						+ "3-Bubble sort\n"
						+ "0-quit\n");
		choice = scan.nextInt();
		
		System.out.println("Please enter the number for a sorting method or 
enter 0 to quit: ");
		int size = scan.nextInt();
		int input[] = new int[size];
		for(int i = 0; i < size; i++) {
			input[i] = scan.nextInt();
		}
		
	    System.out.println("\nBefore Sorting: ");
	    Sorter.printArray(input);
             // sort the array
             Sorter.insertionSort(input);
             System.out.println("\nAfter Sorting: ");
             Sorter.printArray(input);
		
		switch (choice) {
		
		case 0:
			System.out.println("Goodbye!");
			System.exit(0);
			break;
			
		case 1:
			Sorter.insertionSort(input);
         	break;

         }
     }
}

import java.util.Arrays;

public class Sorter {
	
	public static int[] insertionSort(int[] arr) {

		for (int i = 1; i < arr.length; i++) {
             int valueToSort = arr[i];
             int j = i;
             while (j > 0 && arr[j - 1] > valueToSort) {
                 arr[j] = arr[j - 1];
                 j--;
             }//end while loop.
             // insert the element
             arr[j] = valueToSort;
		}//end for loop
             return arr;
	}//end insertionSort		

					
		public static void printArray(int[] arr) {
		    System.out.println(Arrays.toString(arr));			
		}
}	

	

[toc] | [prev] | [next] | [standalone]


#29611

FromJeff Higgins <jeff@invalid.invalid>
Date2014-08-10 00:30 -0400
Message-ID<ls6shk$c40$1@dont-email.me>
In reply to#29610
On 08/10/2014 12:11 AM, Scott Dunning wrote:
> Ok so I realized that I was not filling the array with anything but now
> I'm having issues filling it correctly.  It's only filling the array
> with three of the user inputs and I just cannot figure out why....

I thought you had that sorted over at stackoverflow.
I must have misread that too.

Please, PLEASE, use java.io.Console class. It makes joy.
If your Professor disallows it, understand him as saying:
"F**k me runnin'."

[toc] | [prev] | [next] | [standalone]


#29612

FromJeff Higgins <jeff@invalid.invalid>
Date2014-08-10 00:45 -0400
Message-ID<ls6tdt$f9v$1@dont-email.me>
In reply to#29611
On 08/10/2014 12:30 AM, Jeff Higgins wrote:
> On 08/10/2014 12:11 AM, Scott Dunning wrote:
>> Ok so I realized that I was not filling the array with anything but now
>> I'm having issues filling it correctly.  It's only filling the array
>> with three of the user inputs and I just cannot figure out why....
>
> I thought you had that sorted over at stackoverflow.
> I must have misread that too.
>
> Please, PLEASE, use java.io.Console class. It makes joy.
> If your Professor disallows it, understand him as saying:
> "F**k me runnin'."
>
>
You might also try searching for:
"java fill array with unique random numbers",
or some such.

[toc] | [prev] | [next] | [standalone]


#29615

FromJeff Higgins <jeff@invalid.invalid>
Date2014-08-10 05:58 -0400
Message-ID<ls7fo3$5u6$1@dont-email.me>
In reply to#29612
On 08/10/2014 12:45 AM, Jeff Higgins wrote:
> You might also try searching for:
> "java fill array with unique random numbers",
> or some such.

Well, your first post seemed to be asking for the size of an array to fill.
Your second seems to be asking for a list of values.
I may need sleep.



[toc] | [prev] | [next] | [standalone]


#29614

FromJeff Higgins <jeff@invalid.invalid>
Date2014-08-10 05:54 -0400
Message-ID<ls7fh9$4ns$1@dont-email.me>
In reply to#29611
On 08/10/2014 12:30 AM, Jeff Higgins wrote:
> On 08/10/2014 12:11 AM, Scott Dunning wrote:
>> Ok so I realized that I was not filling the array with anything but now
>> I'm having issues filling it correctly.  It's only filling the array
>> with three of the user inputs and I just cannot figure out why....
>
> I thought you had that sorted over at stackoverflow.
> I must have misread that too.
>
> Please, PLEASE, use java.io.Console class. It makes joy.
> If your Professor disallows it, understand him as saying:
> "F**k me runnin'."
>
Huh, some Java IDEs understand Console the same way!

School begins Monday for our local grade-schoolers,
are you sure you're in a summer class?

import javax.swing.JOptionPane;

public class DriverSort {

   public static void main(String[] args) {

     Object[] sorts = {"Insertion", "Selection", "Bubble"};

     String sortString = (String)JOptionPane.showInputDialog(
       null,
       "Select a sort algorithm\nCancel quits the program",
       "Sort Algorithm Selector",
       JOptionPane.PLAIN_MESSAGE,
       null,sorts,"Insertion");

     if(sortString == null) {
       System.exit(1);
     } else if(sortString != "Insertion"){
       JOptionPane.showMessageDialog(
           null, "Selection and Bubble sorts are not implemented");
       System.exit(2);
     }

     String arrayString = (String)JOptionPane.showInputDialog(
         null,
         "Enter a space separated list of integers\nCancel quits the 
program",
         "The Array to Sort"
         ,JOptionPane.PLAIN_MESSAGE,null,null,"");

     if(arrayString == null) {
       System.exit(3);
     }

     try {
       String[] stringArray = arrayString.split(" ");
       int[] input = new int[stringArray.length];
       for (int i = 0; i < stringArray.length; i++) {
         input[i] = Integer.parseInt(stringArray[i]);
       }
       Sorter.insertionSort(input);
       Sorter.printArray(input);

     } catch (NumberFormatException e) {
       JOptionPane.showMessageDialog(
           null, "Could not parse a list of integers");
       System.exit(4);
     }
   }
}




[toc] | [prev] | [next] | [standalone]


#29616

FromMike Amling <mamling@chaff.us>
Date2014-08-10 06:09 -0500
Message-ID<c4p280F12qiU1@mid.individual.net>
In reply to#29605
On 8/9/14 9:11 PM, Scott Dunning wrote:
> Hello I trying write a sorting program that will ask a user what type of
> sorting method to use (insertion, bubble, selection) and then ask them
> to enter integers to sort.

One wonders why classes are still teaching these algorithms. I've had 
colleagues who have written bubble sort into production code, because 
that's the only algorithm they were ever taught (or maybe it was better 
than some even worse ones).

If you do complete this assignment, throw away the code, look up 
QuickSort, implement a reasonable variation of it, and don't look back.

--Mike Amling
SSdsbCBldmVuIHNlbmQgeW91IGFuIG9wZW4tc291cmNlIGltcGxtZW50YXRpb24u

[toc] | [prev] | [next] | [standalone]


#29619

FromEric Sosman <esosman@comcast-dot-net.invalid>
Date2014-08-10 07:43 -0400
Message-ID<ls7lu3$6ip$1@dont-email.me>
In reply to#29616
On 8/10/2014 7:09 AM, Mike Amling wrote:
> On 8/9/14 9:11 PM, Scott Dunning wrote:
>> Hello I trying write a sorting program that will ask a user what type of
>> sorting method to use (insertion, bubble, selection) and then ask them
>> to enter integers to sort.
>
> One wonders why classes are still teaching these algorithms. I've had
> colleagues who have written bubble sort into production code, because
> that's the only algorithm they were ever taught (or maybe it was better
> than some even worse ones).
>
> If you do complete this assignment, throw away the code, look up
> QuickSort, implement a reasonable variation of it, and don't look back.

     Better still, look up Arrays.sort() and Collections.sort() and
throw away your Quicksort implementation.

-- 
esosman@comcast-dot-net.invalid

[toc] | [prev] | [next] | [standalone]


#29658

FromMike Amling <mamling@chaff.us>
Date2014-08-13 23:51 -0500
Message-ID<c52tikF32e8U1@mid.individual.net>
In reply to#29619
On 8/10/14 6:43 AM, Eric Sosman wrote:
> On 8/10/2014 7:09 AM, Mike Amling wrote:
>> On 8/9/14 9:11 PM, Scott Dunning wrote:
>>> Hello I trying write a sorting program that will ask a user what type of
>>> sorting method to use (insertion, bubble, selection) and then ask them
>>> to enter integers to sort.
>>
>> One wonders why classes are still teaching these algorithms. I've had
>> colleagues who have written bubble sort into production code, because
>> that's the only algorithm they were ever taught (or maybe it was better
>> than some even worse ones).
>>
>> If you do complete this assignment, throw away the code, look up
>> QuickSort, implement a reasonable variation of it, and don't look back.
>
>      Better still, look up Arrays.sort() and Collections.sort() and
> throw away your Quicksort implementation.

How do you get Arrays.sort to sort primitives descending?

What if you want to, say, apply the same permutation to the elements of 
array Y that sorting applies to the elements array X?

--Mike Amling
VG8gc29ydCBpbnRbXSB4IGRlc2NlbmRpbmcsIHJlcGxhY2UgZXZlcnkgZWxlbWVudCB4W2pdIHdp
dGggLTEteFtqXSBhbmQgY2FsbCBBcnJheXMuc29ydCh4KSwgdGhlbiBhZ2FpbiByZXBsYWNlIGV2
ZXJ5IHhbal0gd2l0aCAtMS14W2pdLg==

[toc] | [prev] | [next] | [standalone]


#29664

FromEric Sosman <esosman@comcast-dot-net.invalid>
Date2014-08-14 08:07 -0400
Message-ID<lsi8qf$ubh$1@dont-email.me>
In reply to#29658
On 8/14/2014 12:51 AM, Mike Amling wrote:
> On 8/10/14 6:43 AM, Eric Sosman wrote:
>> On 8/10/2014 7:09 AM, Mike Amling wrote:
>>> On 8/9/14 9:11 PM, Scott Dunning wrote:
>>>> Hello I trying write a sorting program that will ask a user what
>>>> type of
>>>> sorting method to use (insertion, bubble, selection) and then ask them
>>>> to enter integers to sort.
>>>
>>> One wonders why classes are still teaching these algorithms. I've had
>>> colleagues who have written bubble sort into production code, because
>>> that's the only algorithm they were ever taught (or maybe it was better
>>> than some even worse ones).
>>>
>>> If you do complete this assignment, throw away the code, look up
>>> QuickSort, implement a reasonable variation of it, and don't look back.
>>
>>      Better still, look up Arrays.sort() and Collections.sort() and
>> throw away your Quicksort implementation.
>
> How do you get Arrays.sort to sort primitives descending?

	Arrays.sort(theArrayOfPrimitives);
	for (int i = theArrayOfPrimitives.length; --i >= 0; ) {
	    // Note direction of traversal
	}

Or, if you *really* *really* *REEEELY* want the array front-to-back:

	Arrays.sort(theArrayOfPrimitives);
	for (int i = 0, j = theArrayOfPrimitives.length; i < --j; ++i) {
	    PrimitiveType temp = theArrayOfPrimitives[i];
	    theArrayOfPrimitives[i] = theArrayOfPrimitives[j];
	    theArrayOfPrimitives[j] = temp;
	}

> What if you want to, say, apply the same permutation to the elements of
> array Y that sorting applies to the elements array X?

     Whenever I've wanted to do this, it's nearly always turned out
that my data structure design was at fault.  On the remaining few
occasions, I've instead sorted an array of indices.

-- 
esosman@comcast-dot-net.invalid

[toc] | [prev] | [next] | [standalone]


#29661

FromMike Amling <mamling@chaff.us>
Date2014-08-14 00:46 -0500
Message-ID<c530prF3lfrU1@mid.individual.net>
In reply to#29619
On 8/10/14 6:43 AM, Eric Sosman wrote:
> On 8/10/2014 7:09 AM, Mike Amling wrote:
>> On 8/9/14 9:11 PM, Scott Dunning wrote:
>>> Hello I trying write a sorting program that will ask a user what type of
>>> sorting method to use (insertion, bubble, selection) and then ask them
>>> to enter integers to sort.
>>
>> One wonders why classes are still teaching these algorithms. I've had
>> colleagues who have written bubble sort into production code, because
>> that's the only algorithm they were ever taught (or maybe it was better
>> than some even worse ones).
>>
>> If you do complete this assignment, throw away the code, look up
>> QuickSort, implement a reasonable variation of it, and don't look back.
>
>      Better still, look up Arrays.sort() and Collections.sort() and
> throw away your Quicksort implementation.

I also have an implementation of the modification to QuickSort that 
sorts the highest m of n elements into subscripts n-m..n-1 and leaves 
the lowest n-m elements in subscripts 0..n-m-1 but in no particular 
order. It's considerably faster than sorting the entire array, which I 
used to good effect in one game-playing program.

--Mike Amling
SSdtIGp1c3Qgc2F5aW4nLi4u

[toc] | [prev] | [next] | [standalone]


#29739

FromArne Vajhøj <arne@vajhoej.dk>
Date2014-08-19 16:50 -0400
Message-ID<53f3b8b2$0$293$14726298@news.sunsite.dk>
In reply to#29616
On 8/10/2014 7:09 AM, Mike Amling wrote:
> On 8/9/14 9:11 PM, Scott Dunning wrote:
>> Hello I trying write a sorting program that will ask a user what type of
>> sorting method to use (insertion, bubble, selection) and then ask them
>> to enter integers to sort.
>
> One wonders why classes are still teaching these algorithms. I've had
> colleagues who have written bubble sort into production code, because
> that's the only algorithm they were ever taught (or maybe it was better
> than some even worse ones).
>
> If you do complete this assignment, throw away the code, look up
> QuickSort, implement a reasonable variation of it, and don't look back.

During a beginner class the students need to write a lot of code to
get some practical experience and learn to find errors.

And most likely they should also in the theoretical part learn about
algorithms and big O analysis.

Sorting is one out of maybe a handful of areas suitable for achieving
both.

Arne

[toc] | [prev] | [next] | [standalone]


#29640

FromRoedy Green <see_website@mindprod.com.invalid>
Date2014-08-12 03:00 -0700
Message-ID<tcpju9p284tdlo67l3a22jjri793lkm44c@4ax.com>
In reply to#29605
On Sat, 09 Aug 2014 19:11:10 -0700, Scott Dunning
<scottwd01@gmail.com> wrote, quoted or indirectly quoted someone who
said :

>Hello I trying write a sorting program that will ask a user what type of 
>sorting method to use (insertion, bubble, selection) and then ask them 
>to enter integers to sort.

I have posted the source code to a number of sorts.

See http://mindprod.com/sort.html
-- 
Roedy Green Canadian Mind Products http://mindprod.com
The art of strongly-typed language design is largely arranging 
that errors are automatically detected as soon as possible
in the compose, compile, run cycle.

[toc] | [prev] | [next] | [standalone]


#29659

FromMike Amling <mamling@chaff.us>
Date2014-08-13 23:51 -0500
Message-ID<c52tiqF32e8U2@mid.individual.net>
In reply to#29640
On 8/12/14 5:00 AM, Roedy Green wrote:
> On Sat, 09 Aug 2014 19:11:10 -0700, Scott Dunning
> <scottwd01@gmail.com> wrote, quoted or indirectly quoted someone who
> said :
>
>> Hello I trying write a sorting program that will ask a user what type of
>> sorting method to use (insertion, bubble, selection) and then ask them
>> to enter integers to sort.
>
> I have posted the source code to a number of sorts.
>
> See http://mindprod.com/sort.html

Do you access your web site by some path not available to the rest of 
us? How is it that you never notice 404s until someone points them out?

--Mike Amling
SnVzdCBjdXJpb3VzLg==

[toc] | [prev] | [next] | [standalone]


#29663

FromJukka Lahtinen <jtfjdehf@hotmail.com.invalid>
Date2014-08-14 14:30 +0300
Message-ID<lvy4ur8g1c.fsf@saunalahti.fi>
In reply to#29659
Mike Amling <mamling@chaff.us> writes:
> On 8/12/14 5:00 AM, Roedy Green wrote:

>> I have posted the source code to a number of sorts.
>> See http://mindprod.com/sort.html

> Do you access your web site by some path not available to the rest of us?
> How is it that you never notice 404s until someone points them out?

Maybe he just forgets to use the chmod command to make the files
world-readable.

-- 
Jukka Lahtinen

[toc] | [prev] | [standalone]


Back to top | Article view | comp.lang.java.programmer


csiph-web