Database System Concepts
Database System Concepts
7th Edition
ISBN: 9780078022159
Author: Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher: McGraw-Hill Education
Bartleby Related Questions Icon

Related questions

bartleby

Concept explainers

Question

java

 


final static Random rand = new Random(); final static int MAX_SIZE = 10; // amount of storage for the intList static int size = 0; // number of values actually in the intList static int[] intList = new int[MAX_SIZE]; /** * @param args the command line arguments */ public static void main(String[] args) { out.println("CPS 151 In-class assignment 2 by _______________________"); setRandom(intList, 100); printList(intList, MAX_SIZE, "\nArray before sorting"); sort(intList); printList(intList, MAX_SIZE, "\nArray after sorting"); } // end main // prints partially filled array with a legend private static void printList(final int[] arr, final int size, final String legend) { out.println(legend); for (int k = 0; k < size; k++) { out.print(" " + arr[k]); } out.println(); } // end printList // move items from pos:size-1 one position down (higher subscripts) private static void shiftDown(int[] arr, int size, int pos) { // Write the code (use code from ICA 1) } // end shiftDown private static void setRandom(int[] arr, final int range) { for (int k = 0; k < arr.length; k++) { arr[k] = rand.nextInt(range); } } // end setRandom private static void sort(int[] arr) { for (int k = 1; k < arr.length; k++) { // assume arr[0:k-1] sorted int save = arr[k]; // arr[k] may be overwritten later, so save that value int pos = findPos(arr, k, save); // find where to insert save in arr[0:k-1] shiftDown(arr, k, pos); arr[pos] = save; // arr[0:k] is now sorted with k+1 values printList(arr, MAX_SIZE, "\nAfter pass " + k); } // end for } // end sort

±
21:01
६६/-/\\
!
ENG
This assignment uses some concepts developed in ICA 1. In particular, the shiftDown method developed for
ICA 1 can be reused.
When completed, this program will create a random array with 10 integer values and sort it using Insertion sort
technique. Most of the code is written. You should be able to reuse code for shiftDown method. You need to
complete the findPos method, following the description given later.
Submit the source code and screen shot as described in the document "How to submit assignments" through
Isidore. Follow naming conventions as already described.
6
18
35
45
56
77
91
Design of "find Pos" method
The findPos method is used to find the right place to insert a new value in a partially filled array. The partially filled
array is assumed to be sorted in increasing order and if the new value is inserted at the index position (as calculated by
findPos) then the resulting array will remain sorted. This is the method specification given for findPos:
// find the right place for item to be inserted within arr [0:size-1]
private static int findPos (int[] arr, int size, int item) {
(12, 23, 45, 67, 88). Note that there may be more elements in axr but as a
Suppose size = 5 and arr =
partially filled array, only 5 values are relevant. These are the values returned by the method for various possible values
of item.
item
@ A
A- F2
return value from findPos (arr, 5, item)
0
12 Y
W
1
2
3 (new 45 goes after old 45's)
3
34
4
5
You can start a position variable at 0 and keep incrementing it as long as the position is < size and item is >
arr[position]. The final value is the return value from the method. Note: It is also possible to write the loop backward.
OEI
#
3
F3
E
X
+
F4
$
4
с
75%
F5
R
团
F6
T
0
%
50
E
1 / 2
F7
FB
&
7 V
6 17
Y
100
D
U
8
FO
expand button
Transcribed Image Text:± 21:01 ६६/-/\\ ! ENG This assignment uses some concepts developed in ICA 1. In particular, the shiftDown method developed for ICA 1 can be reused. When completed, this program will create a random array with 10 integer values and sort it using Insertion sort technique. Most of the code is written. You should be able to reuse code for shiftDown method. You need to complete the findPos method, following the description given later. Submit the source code and screen shot as described in the document "How to submit assignments" through Isidore. Follow naming conventions as already described. 6 18 35 45 56 77 91 Design of "find Pos" method The findPos method is used to find the right place to insert a new value in a partially filled array. The partially filled array is assumed to be sorted in increasing order and if the new value is inserted at the index position (as calculated by findPos) then the resulting array will remain sorted. This is the method specification given for findPos: // find the right place for item to be inserted within arr [0:size-1] private static int findPos (int[] arr, int size, int item) { (12, 23, 45, 67, 88). Note that there may be more elements in axr but as a Suppose size = 5 and arr = partially filled array, only 5 values are relevant. These are the values returned by the method for various possible values of item. item @ A A- F2 return value from findPos (arr, 5, item) 0 12 Y W 1 2 3 (new 45 goes after old 45's) 3 34 4 5 You can start a position variable at 0 and keep incrementing it as long as the position is < size and item is > arr[position]. The final value is the return value from the method. Note: It is also possible to write the loop backward. OEI # 3 F3 E X + F4 $ 4 с 75% F5 R 团 F6 T 0 % 50 E 1 / 2 F7 FB & 7 V 6 17 Y 100 D U 8 FO
•1:01
EE/ TAI
!
1
ENG
B
F1
1
Sample Output (12 point font used for Output pane)
2
al
Output-ICA02_Key (run) X
D
DD
DIL
F2
۲
W
run:
CPS 151 In-class assignment 2 by KEY
After pass 2:
Array before sorting: 35 22 60 79 44 63 99 34 19 30
After pass 1: 22 35 60 79 44 63 99 34 19 30
After pass 3:
After pass 4:
After pass 6:
After pass 7:
After pass 8:
(@A
After pass 9:
After pass 5: 22 35 44 60 63 79 99 34 19 30
English
GE
A+
Yoe
#3
F3
22 35 60 79 44 63 99 34 19 30
22 35 60 79 44 63 99 34 19 30
22 35 44 60 79 63 99 34 19 30
22 35 44 60 63 79 99 34 19 30
Array after sorting: 19 22 30 34 35 44 60 63 79 99
BUILD SUCCESSFUL (total time: 0 seconds)
22 34 35 44 60 63 79 99 19 30
19 22 34 35 44 60 63 79 99 30
19 22 30 34 35 44 60 63 79 99
E
X
F4
$
4 E
O
R
F5
%
5
2
F6
T
+
O
75%
X
F7
<
B
C
F8
2 / 2
8
0617 V8
Y U
expand button
Transcribed Image Text:•1:01 EE/ TAI ! 1 ENG B F1 1 Sample Output (12 point font used for Output pane) 2 al Output-ICA02_Key (run) X D DD DIL F2 ۲ W run: CPS 151 In-class assignment 2 by KEY After pass 2: Array before sorting: 35 22 60 79 44 63 99 34 19 30 After pass 1: 22 35 60 79 44 63 99 34 19 30 After pass 3: After pass 4: After pass 6: After pass 7: After pass 8: (@A After pass 9: After pass 5: 22 35 44 60 63 79 99 34 19 30 English GE A+ Yoe #3 F3 22 35 60 79 44 63 99 34 19 30 22 35 60 79 44 63 99 34 19 30 22 35 44 60 79 63 99 34 19 30 22 35 44 60 63 79 99 34 19 30 Array after sorting: 19 22 30 34 35 44 60 63 79 99 BUILD SUCCESSFUL (total time: 0 seconds) 22 34 35 44 60 63 79 99 19 30 19 22 34 35 44 60 63 79 99 30 19 22 30 34 35 44 60 63 79 99 E X F4 $ 4 E O R F5 % 5 2 F6 T + O 75% X F7 < B C F8 2 / 2 8 0617 V8 Y U
Expert Solution
Check Mark
Knowledge Booster
Background pattern image
Computer Science
Learn more about
Need a deep-dive on the concept behind this application? Look no further. Learn more about this topic, computer-science and related others by exploring similar questions and additional content below.
Similar questions
Recommended textbooks for you
Text book image
Database System Concepts
Computer Science
ISBN:9780078022159
Author:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:McGraw-Hill Education
Text book image
Starting Out with Python (4th Edition)
Computer Science
ISBN:9780134444321
Author:Tony Gaddis
Publisher:PEARSON
Text book image
Digital Fundamentals (11th Edition)
Computer Science
ISBN:9780132737968
Author:Thomas L. Floyd
Publisher:PEARSON
Text book image
C How to Program (8th Edition)
Computer Science
ISBN:9780133976892
Author:Paul J. Deitel, Harvey Deitel
Publisher:PEARSON
Text book image
Database Systems: Design, Implementation, & Manag...
Computer Science
ISBN:9781337627900
Author:Carlos Coronel, Steven Morris
Publisher:Cengage Learning
Text book image
Programmable Logic Controllers
Computer Science
ISBN:9780073373843
Author:Frank D. Petruzella
Publisher:McGraw-Hill Education