Header

divider
Links
• Home
• Contact Me
• 
Aeries Portal
• Royal High School
• Simi Valley USD

divider

AP Statistics

• Course Calendar
• Chapter Notes
• Practice Tests
• Statistics Links

divider
AP Comp-Sci
 Course Content
• Programs
• Practice Tests
• Comp-Sci Links

divider

CP Statistics
• Course Calendar
• Chapter Notes
• Practice Tests
• Statistics Links


divider

Algebra
• Table of Contents
Geometry
• Table of Contents

Math Power
• Table of Contents

AP Computer Science Unit 5 Programs
Manipulating Data: Sorting, Searching and Recursion

 
  WORKING WITH SORTING ALGORITHMS

(1)   Using the Bubble Sort: Randomly generate a number of integers (chosen by the user) in the range 1-100 and store them in an array. Display the list of integers generated. Then sort them in descending order. Finally, redisplay the integers. See the example below.

Sample run of the program (input an output values in bold):

How many integers to generate: 10

Before sort: 15 28 89 52 71 48 18 65 32 10

After sort: 89 71 65 52 48 32 28 18 15 10

 

(2)   Using the Selection Sort: Enter and store 5 grade point averages in an array of type double. Then sort them in ascending order. Finally, redisplay the GPA’s. See the example below.

Sample run of the program (input an output values in bold):

Enter GPA #1: 3.78

Enter GPA #2: 3.07

Enter GPA #3: 2.93

Enter GPA #4: 4.25

Enter GPA #5: 2.52

GPA’s Sorted: 2.52, 2.93, 3.07, 3.78, 4.25

(3) Using the Insertion Sort: Enter and store 5 words in an ArrayList of type String. Then sort them in alphabetical (ascending) order. Finally, redisplay the words. See the example below.

Sample run of the program (input an output values in bold):

Enter word #1: delirium

Enter word #2: trigger

Enter word #3: time

Enter word #4: consumer

Enter word #5: second

Alphabetized:

consumer
delirium
second
time
trigger

(4)   Use any sort: Read the numbers out of the file numbers.txt, then sort and display them in descending order. See form below.

Sample run of the program (input an output values in bold):

Before sort: 5 8 2 7 6 3 6 8 5 4 9 4 7 7 6

After sort: 9 8 8 7 7 7 6 6 6 5 5 4 4 3 2

 

(5)   Use any sort: Read the names out of the file names.txt, then sort and display them in alphabetical order. Use an ArrayList to store the names. See form below.

Sample run of the program (input an output values in bold):

Baker
Cey
Garvey
etc...


(6)  Use any sort: Enter a single word and store in a String. Then sort the letters of the word in alphabetical (ascending ) order. Finally, redisplay the new order of the letters. See the example below. Hint: you will need to copy the characters of the String into an array of characters in order to perform the sort.

Sample run of the program (input an output values in bold):

Enter a word: turbine

Alphabetical order: beinrtu

 

(7) Sorting Students: Allow the user to sort the contents of the students.txt file by:

1 - Last Name (alphabetical)
2 - Average test score (descending)

Load the data into an ArrayList and allow the user to make thir choice, then sort and display as shown in the example below:

Name Sort
Adams  78.5
Bonilla  82.5
etc...

Average Sort
Smith  95.4
Gaines  92.7
etc...

 

 

 

 

 

 

 

 

Home  •  About Me  •  Aeries Portal  •  Contact Me
© DanShuster.com