• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Junilu Lacar
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • Jeanne Boyarsky
  • Rob Spoor
  • Bear Bibeault
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Piet Souris
  • Carey Brown
  • Stephan van Hulst
Bartenders:
  • Frits Walraven
  • fred rosenberger
  • salvin francis

Overwriting an array.

 
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi guys, I'm constructing a program in which I generate a random array of 1000 integers, and then sort them via three methods, Bubble Sort, Insertion Sort and Quick Sort and then calculate the time of execution. I have managed to finish the majority of the program, however I'm having a problem in trying to overwrite the sorted array with the unsorted one in order to pass it down to the next method, in order to have fair execution times. I managed to copy the unsorted array, however all I need to do now is overwrite, do any of you have any suggestions please?

Thanks
 
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Welcome to the Ranch.

Can you show us your code? It would be much easier to help you if you show us exactly what the problem with the code is.
 
Stephane Bonett
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
There you go.

 
Marshal
Posts: 73267
332
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
My, there’s some hard to read code there. Single-letter identifiers should only be used for loop indices or formal type parameters.
Do you really want to print “this is the unsorted array” 1000 times?
You ought not to try to pass the size of an array as an argument, but should always use the array’s length field directly inside the method.
What is wrong with using the clone() method on your array? It returns a shallow copy, but when the elements are primitives, that will be all right. Why overwrite the arrays? Why not simply clone it twice, and you will then have three copies.
By the way: for a 1000-element array the millisecond method probably doesn’t have a fine enough granularity for your timing. There is a System class (static) method which returns nanoseconds or some approximation to nanoseconds. You will probably have to use that.
 
Stephane Bonett
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I have done as you suggested Campbell, however I'm getting a 'cannot find symbol' error for each new clone array.

 
Campbell Ritchie
Marshal
Posts: 73267
332
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
That is because you have put them in their own block and they have gone out of scope. By the way: you need to clone each new array once, not 1000 times!
 
Stephane Bonett
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Oops, my bad, thanks for pointing that out. Regarding the method you referred to about nanoseconds, do you mind explaining, as the time of executions are all the same.

EDIT: Nevermind, I managed to make it work.

Thanks for your input Campbell.
 
Ranch Hand
Posts: 808
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Some suggestions to make your code easier to read:

Apply indentation of code consistently. Indentation should reflect the code structure, making it easier to read and understand.
Always, always use {} braces for loops and conditionals, even if there is only one line of instruction inside the braces.
Repetitious code should be refactored, extracting the repeated lines into a method.
Yes, the language allows you to declare and initialize multiple variables on a single line. But don't.
 
Campbell Ritchie
Marshal
Posts: 73267
332
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Stephane Bonett wrote:. . .

Thanks for your input Campbell.

You’re welcome
 
You showed up just in time for the waffles! And this tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
reply
    Bookmark Topic Watch Topic
  • New Topic