IN JAVA please and a picture of result and the excell work sheet would be nice.
ID: 3728415 • Letter: I
Question
IN JAVA please and a picture of result and the excell work sheet would be nice. Thank you.
1.Write a computer program that prompts the user for a number, creates an array for that number of random integers, and then use the bubble sort to order the array. The program should print out the array prior to the call to the sorting algorithm and afterwards. You can write the program in either Java, C++, C#, or whatever language you are most comfortable in.
2. Repeat 1 but use selection sort this time.
3. Write a computer program that prompts the user for one number, n for the number of items in the array to sort, and create and sort 1000 arrays of this size timing the run to get an average time to sort an array of this size. Then do the following:
Initiate a variable running_time to 0
Create a for loop that iterates 1000 times.
In the body of the loop,
Create an array of n random integers
Get the time and set this to start-time. You will have to figure out what the appropriate command is in the programming language you are using to find the time (Important: Do not start the timer until after the array is created).
Use bubble sort to sort the array
Get the time and set this to end-time Subtract start-time from end-time and add the result to total_time
Once the program has run, note
The number of items sorted
The average running time for each array (total_time/1000)
Repeat the process six times, using 500, 2500 and 5000 as the size of the array for each of the two algorithms.
4. Repeat 3 using selection sort.
5. Create a spreadsheet showing the results of 3 and 4 and create a graph to graphically represent the information. Show both sort algorithms on the same graph for comparison.
Explanation / Answer
Create solution for whatever the user input the size the array will create the size of the array and the randondom number will generate from to number enter by the user and then sort using bubble sort and selection sort
package chegg;
import java.text.DecimalFormat;
import java.text.NumberFormat;
import java.util.Scanner;
public class BubbleSort {
public static void main(String[] args) {
NumberFormat formatter = new DecimalFormat("#0.00000"); //format the number
int numberofsize, key = 0; //numberofsize for array size and key for search key in array
long start,end; //for calculate the running time of search key
Scanner input = new Scanner(System.in); //scanner for input
System.out.print("Input Array Size: "); //asking the user to input number
numberofsize = input.nextInt(); //store input in numberofsize
int arr[] = new int[numberofsize]; //set the array size
for(int i=0;i<numberofsize;i++){ //run the loop to generate the array integer
arr[i] = (int)(Math.random()*numberofsize) + 1; //generate random integer in 1-1000 (if change 1000 to any number it will generate the number upto that number)
System.out.println(" "+arr[i]); //store the every generate random number to arr integer
}
start = System.currentTimeMillis();//running current time
bubbleSort(arr); //create a bubbleSort method and pass the arr into it to sort the array
end = System.currentTimeMillis();//running current time
System.out.println("Sorting time taken is: " + formatter.format((end - start) / 1000) + " seconds"); //calculate the running time end time minus start time
System.out.println("Sorted array"); //print the sorted array
printArray(arr); //prints the sorted array using printArray method
//selection sort
System.out.print("Input Array Size: "); //asking the user to input number
numberofsize = input.nextInt(); //store input in numberofsize
int arr1[] = new int[numberofsize]; //set the array size
for(int i=0;i<numberofsize;i++){ //run the loop to generate the array integer
arr1[i] = (int)(Math.random()*numberofsize) + 1; //generate random integer in 1-1000 (if change 1000 to any number it will generate the number upto that number)
System.out.println(" "+arr1[i]); //store the every generate random number to arr integer
}
start = System.nanoTime();//running current time
selectionSort(arr1); //create a bubbleSort method and pass the arr into it to sort the array
end = System.nanoTime();//running current time
System.out.println("Sorting time taken is: " + formatter.format((end - start) / 1000) + " seconds"); //calculate the running time end time minus start time
System.out.println("Sorted array"); //print the sorted array
printArray(arr1); //prints the sorted array using printArray method
}
public static void selectionSort(int[] arr) {
int n = arr.length;
// One by one move to next unsorted array
for (int i = 0; i < n-1; i++)
{
// Find the minimum element in unsorted array
int minvalue = i;
for (int j = i+1; j < n; j++)
if (arr[j] < arr[minvalue])
minvalue = j;
// Swap the found minimum element with the first
// element
int temp = arr[minvalue];
arr[minvalue] = arr[i];
arr[i] = temp;
}
}
public static void printArray(int[] arr) {
for (int i=0; i<arr.length; ++i) //loop to length of array
System.out.println(arr[i]); // prints the array
System.out.println();
}
public static void bubbleSort(int[] arr) { //sort the array using bubble sort
int n = arr.length; //length of array in n variable
for (int i = 0; i < n-1; i++) //loop variable array -1 size
for (int j = 0; j < n-i-1; j++) //loop array variable -i-1
if (arr[j] > arr[j+1]) //check if array of j index value is greater than array of j+1
{
// swap temp and arr[i]
int temp = arr[j];
arr[j] = arr[j+1];
arr[j+1] = temp;
}
}
}
sample output:
Input Array Size: 10
7
10
9
9
9
6
9
8
4
1
Sorting time taken is: 0.00000 seconds
Sorted array
1
4
6
7
8
9
9
9
9
10
Input Array Size: 1000
179
841
36
892
302
115
924
422
684
564
886
150
107
537
108
575
944
968
405
716
704
573
693
614
177
728
338
660
863
941
740
721
570
131
481
759
15
244
598
839
40
89
67
142
552
657
357
904
653
618
28
992
480
106
674
169
172
415
173
962
40
707
123
456
449
400
552
265
126
454
464
384
650
375
559
630
609
428
517
906
519
151
545
559
915
18
529
714
163
796
985
574
448
902
210
316
77
867
477
409
711
818
835
882
386
767
123
322
222
347
83
141
520
589
47
980
566
234
518
923
798
99
127
602
197
991
868
139
652
714
185
307
817
793
527
290
439
623
198
849
443
911
918
798
103
413
307
192
508
194
650
745
111
843
652
613
591
351
609
817
436
638
329
264
622
40
414
540
516
431
999
396
666
9
870
106
444
234
929
693
628
804
366
372
842
73
518
380
481
795
337
830
146
646
566
78
854
511
235
635
285
163
845
578
100
565
644
988
960
718
750
978
164
643
468
454
12
843
55
887
868
330
337
24
585
103
73
614
458
772
651
994
73
890
753
560
501
429
918
972
61
756
486
230
491
412
300
574
742
955
673
578
754
182
520
645
526
34
330
609
504
588
546
46
813
641
269
440
144
516
710
345
164
890
632
256
169
643
178
969
325
942
827
859
247
58
918
623
844
666
139
738
358
748
587
72
239
514
560
870
56
203
999
563
951
983
616
680
224
651
71
492
547
306
228
849
911
662
423
104
904
229
898
929
97
142
30
305
331
32
747
937
490
966
591
903
875
144
192
375
697
739
533
839
258
998
306
381
369
464
785
227
189
3
131
927
168
99
770
272
15
762
492
282
652
273
239
379
923
899
92
159
425
652
879
17
193
159
917
252
157
716
850
164
191
618
255
105
312
956
653
389
104
164
841
483
683
726
7
890
622
617
927
725
88
278
624
61
45
104
312
726
826
719
58
398
536
597
610
526
721
893
30
804
500
714
301
677
881
451
895
633
299
177
334
838
498
608
156
8
785
553
343
281
293
336
183
253
377
160
732
262
458
10
559
495
636
254
185
437
185
743
701
760
586
899
378
768
135
934
669
112
234
125
930
616
446
764
327
682
21
266
875
955
120
657
267
613
176
606
196
896
894
955
142
280
187
709
198
978
940
310
346
319
883
63
768
896
994
548
166
294
816
915
789
626
753
14
849
75
955
434
263
128
653
639
222
224
879
567
909
67
357
247
786
980
964
227
292
256
572
846
436
641
284
696
742
747
19
917
229
412
185
354
629
768
674
940
850
45
772
137
752
955
618
676
688
829
775
328
888
553
91
949
684
745
503
580
795
546
607
940
363
863
890
291
190
893
220
235
707
282
824
712
489
322
288
146
779
195
246
409
355
364
344
45
256
885
745
972
328
577
904
722
295
718
613
93
77
145
325
27
367
461
591
386
59
298
51
283
996
478
765
587
618
240
854
235
545
625
356
11
811
530
903
549
462
913
355
390
497
107
777
86
661
256
241
475
472
399
753
488
917
863
246
33
687
203
980
805
477
99
891
111
831
294
374
179
253
876
773
703
817
931
345
837
299
894
814
193
960
716
692
897
441
294
452
210
872
502
270
734
261
544
499
765
366
274
277
925
810
622
628
808
905
210
255
9
736
815
725
573
379
357
154
582
718
692
566
357
244
610
58
469
379
78
555
590
310
951
424
982
86
97
481
341
712
111
916
520
292
577
529
640
853
130
208
7
330
425
11
226
345
478
326
937
829
739
544
624
246
696
400
8
152
770
636
666
114
743
903
620
519
14
926
834
680
486
744
728
725
28
525
64
154
32
797
810
621
759
195
877
470
836
723
948
750
483
340
975
993
789
179
286
816
643
21
684
475
706
764
353
892
172
475
855
293
927
293
27
515
102
867
127
268
964
261
121
99
542
230
660
580
377
575
859
305
320
833
728
573
336
918
741
295
885
438
796
495
550
337
557
19
247
180
967
439
506
898
543
953
63
25
913
37
212
517
939
941
152
53
689
681
883
300
459
53
19
15
42
314
265
2
416
630
214
897
105
68
530
446
506
906
567
475
592
297
945
5
525
885
646
55
895
770
746
485
486
372
974
903
684
82
919
20
932
327
852
65
909
553
815
814
414
944
824
557
333
150
805
838
406
120
124
150
956
231
409
818
165
222
664
221
986
131
590
129
267
315
370
391
208
876
159
420
851
256
946
921
902
578
471
93
806
322
378
100
682
272
877
956
410
274
739
76
876
243
592
676
178
18
118
879
338
132
946
857
923
286
737
Sorting time taken is: 6041.00000 seconds
Sorted array
2
3
5
7
7
8
8
9
9
10
11
11
12
14
14
15
15
15
17
18
18
19
19
19
20
21
21
24
25
27
27
28
28
30
30
32
32
33
34
36
37
40
40
40
42
45
45
45
46
47
51
53
53
55
55
56
58
58
58
59
61
61
63
63
64
65
67
67
68
71
72
73
73
73
75
76
77
77
78
78
82
83
86
86
88
89
91
92
93
93
97
97
99
99
99
99
100
100
102
103
103
104
104
104
105
105
106
106
107
107
108
111
111
111
112
114
115
118
120
120
121
123
123
124
125
126
127
127
128
129
130
131
131
131
132
135
137
139
139
141
142
142
142
144
144
145
146
146
150
150
150
151
152
152
154
154
156
157
159
159
159
160
163
163
164
164
164
164
165
166
168
169
169
172
172
173
176
177
177
178
178
179
179
179
180
182
183
185
185
185
185
187
189
190
191
192
192
193
193
194
195
195
196
197
198
198
203
203
208
208
210
210
210
212
214
220
221
222
222
222
224
224
226
227
227
228
229
229
230
230
231
234
234
234
235
235
235
239
239
240
241
243
244
244
246
246
246
247
247
247
252
253
253
254
255
255
256
256
256
256
256
258
261
261
262
263
264
265
265
266
267
267
268
269
270
272
272
273
274
274
277
278
280
281
282
282
283
284
285
286
286
288
290
291
292
292
293
293
293
294
294
294
295
295
297
298
299
299
300
300
301
302
305
305
306
306
307
307
310
310
312
312
314
315
316
319
320
322
322
322
325
325
326
327
327
328
328
329
330
330
330
331
333
334
336
336
337
337
337
338
338
340
341
343
344
345
345
345
346
347
351
353
354
355
355
356
357
357
357
357
358
363
364
366
366
367
369
370
372
372
374
375
375
377
377
378
378
379
379
379
380
381
384
386
386
389
390
391
396
398
399
400
400
405
406
409
409
409
410
412
412
413
414
414
415
416
420
422
423
424
425
425
428
429
431
434
436
436
437
438
439
439
440
441
443
444
446
446
448
449
451
452
454
454
456
458
458
459
461
462
464
464
468
469
470
471
472
475
475
475
475
477
477
478
478
480
481
481
481
483
483
485
486
486
486
488
489
490
491
492
492
495
495
497
498
499
500
501
502
503
504
506
506
508
511
514
515
516
516
517
517
518
518
519
519
520
520
520
525
525
526
526
527
529
529
530
530
533
536
537
540
542
543
544
544
545
545
546
546
547
548
549
550
552
552
553
553
553
555
557
557
559
559
559
560
560
563
564
565
566
566
566
567
567
570
572
573
573
573
574
574
575
575
577
577
578
578
578
580
580
582
585
586
587
587
588
589
590
590
591
591
591
592
592
597
598
602
606
607
608
609
609
609
610
610
613
613
613
614
614
616
616
617
618
618
618
618
620
621
622
622
622
623
623
624
624
625
626
628
628
629
630
630
632
633
635
636
636
638
639
640
641
641
643
643
643
644
645
646
646
650
650
651
651
652
652
652
652
653
653
653
657
657
660
660
661
662
664
666
666
666
669
673
674
674
676
676
677
680
680
681
682
682
683
684
684
684
684
687
688
689
692
692
693
693
696
696
697
701
703
704
706
707
707
709
710
711
712
712
714
714
714
716
716
716
718
718
718
719
721
721
722
723
725
725
725
726
726
728
728
728
732
734
736
737
738
739
739
739
740
741
742
742
743
743
744
745
745
745
746
747
747
748
750
750
752
753
753
753
754
756
759
759
760
762
764
764
765
765
767
768
768
768
770
770
770
772
772
773
775
777
779
785
785
786
789
789
793
795
795
796
796
797
798
798
804
804
805
805
806
808
810
810
811
813
814
814
815
815
816
816
817
817
817
818
818
824
824
826
827
829
829
830
831
833
834
835
836
837
838
838
839
839
841
841
842
843
843
844
845
846
849
849
849
850
850
851
852
853
854
854
855
857
859
859
863
863
863
867
867
868
868
870
870
872
875
875
876
876
876
877
877
879
879
879
881
882
883
883
885
885
885
886
887
888
890
890
890
890
891
892
892
893
893
894
894
895
895
896
896
897
897
898
898
899
899
902
902
903
903
903
903
904
904
904
905
906
906
909
909
911
911
913
913
915
915
916
917
917
917
918
918
918
918
919
921
923
923
923
924
925
926
927
927
927
929
929
930
931
932
934
937
937
939
940
940
940
941
941
942
944
944
945
946
946
948
949
951
951
953
955
955
955
955
955
956
956
956
960
960
962
964
964
966
967
968
969
972
972
974
975
978
978
980
980
980
982
983
985
986
988
991
992
993
994
994
996
998
999
999