• Post Reply Bookmark Topic Watch Topic
  • New Topic

How Do I Sort A Random Java Array?  RSS feed

 
Erika Her
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So, I'm writing a program that will generate a random array. This array has to be sorted into ascending order. The size of the array depends on what the user inputs for the array size (when prompted). I figured out how to generate a random array, but I'm not sure how to sort the array into ascending order. We are not allowed to array.sort();, so I'm not really sure where to start. Any tips would be nice! Thanks!

Here's what I have so far:

 
Nguyen Tuyen
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi, there are many of sort algorithms to sort your array: binary sort, bubble sort, or selection sort...
 
Stephan van Hulst
Saloon Keeper
Posts: 7969
143
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Assuming you may not use any kind of sorting method from the standard API (including List.sort()), you will have to write your own sorting algorithm.

I recommend implementing a merge sort, but if you're not comfortable with recursion yet, go for insertion sort. Either way, here is a method signature you can use for both:
 
Campbell Ritchie
Marshal
Posts: 56529
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Try this link, or Wikipedia. The first link is fun to watch.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!