Page 1 of 1

Purpose: - Write a Java program - Use command-line arguments - Use arrays, write sorting algorithms.

Posted: Sun May 15, 2022 10:24 am
by answerhappygod
Purpose: - Write a Java program
- Use command-line arguments
- Use arrays, write sorting algorithms.

Note:
Two video lectures are posted on D2L
under "Content"
*** Go to the content page and you ***
*** will see: ***
*** - Sorting #1 ***
*** - Sorting #2 ***

Watch the two videos and do the following:
- Try sorting a small list (as seen in the video)
by hand to convince yourself that all three of
the sorts will work as described
{Pseudocode for each of the sorts is included}
{below, use it as a reference. }

Description:

Write four array processing functions:
- a function to load an integer array with a random
set of integers
- a function to sort an integer array using bubble
sort
- a function to sort an integer array using selection
sort
- a function to sort an integer array using insertion
sort

Write a test program that reads in one command-line
argument that is the size of an array to create. Have
the program create the array, and load the array with
random values.

{ Keep a copy of this original array so that all }
{ sorts will be sorting the same initial set of }
{ values. }

Call each of the sort functions with a copy of the
original array of random values, to show that all of
the algorithms are capable of sorting the same set of
values.


- For conversion of String data to a primitive data
type, use a Wrapper class

- To generate a random number in Java use:

Math.random( )

Math.random( ) produces a double value from 0.0 to,
but not including 1.0

To turn this into some useful values you can scale
it up by multiplying it by a scale factor:

Math.random( ) * 1000

will produce values between [0.0, 1000.0) and you
can turn those into 1000 different integer values
by casting the result it to an integer:

(int)(Math.random( ) * 1000)

which produces integers in the range [0,999]

Notes:
-----
- Write a main( ) to exercise your functions and to
illustrate that the functions are working properly.

- Print the values in the array, before and after
each sort, to show that they are working on the
same set of values, and that the sorting actually
works.

- Nicely format and label your output

E-mail your source code to Otto.
=======================================================
*** Pseudocode for sorts: ***

Bubble sort:
===========
pass = 0
while pass < SIZE - 1 do:

item = SIZE - 1
while item > pass do:
if ar[item] > ar[item - 1]
then
temp = ar[item]
ar[item] = ar[item - 1]
ar[item - 1] = temp
end if

item = item - 1

end while

pass = pass + 1

end while
-------------------------------------------


Selection sort:
==============
pass = 0
while pass < SIZE - 1 do:

position = pass

item = pass + 1
while item < SIZE do:
if ar[position] < ar[item]
then
position = item
end if
item = item + 1

end while

if pass != position
temp = ar[pass]
ar[pass] = ar[position]
ar[position] = temp
end if

pass = pass + 1

end while
-------------------------------------------


Insertion sort:
==============
ordered = 0

pass = ordered
while pass < SIZE - 1 do:

item = pass + 1
while (item > 0) AND (ar[item] > ar[item - 1]) do:
temp = ar[item - 1]
ar[item - 1] = ar[item]
ar[item] = temp
item = item - 1

end while

pass = pass + 1

end while