Header

divider
Links
• Home
• Webgrades
• Contact Me

• Royal High School
• Simi Valley USD


divider

AP Statistics

• Homework Log
• Test Schedule
• Chapter Reviews
• Practice Tests
• Research Project
• Statistics Links

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

divider

AP Calculus AB
• Homework Log
• Test Schedule
• Chapter Reviews
• Practice Tests
• Calculus Links


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 array 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. 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

 

 

  WORKING WITH SEARCH ALGORITHMS

(1)   Write a program that loads an integer array with 25 random integers in the range 1-100. Then ask the user for an integer to search for in the array. The program should use a linear search to find and display the index in the array where that integer first occurs. If the integer does not occur in the array, then a message saying so should be displayed.

 

(2) Write a program to read a list of names from a text file, h:\\txtfiles\\namefile.txt, into an array of type String. You will then prompt the user to enter a name and the program will search the array (using a linear search) and display the index of that name in the array. If the name is not found, then display an appropriate message stating so. Note: all names will need to be converted to uppercase in order for this comparison to be made.

Test Data and Output Format

The name JOHNSON is in position 5

The name WILSON is not in the array


(3) After successfully running the program #1, do a SAVE AS and modify the program so that it will do the search using a binary search algorithm. Recall that this requires that the array first be sorted in ascending order. The output format should be the same as before.


(4)  Now modify program #2 (do a SAVE AS) so that it will first sort the list of names before prompting the user for a name to search for. Then use a binary search to search for the name. Again, an error message should be displayed if the name is not found.

Test Data and Output Format

The name GARCIA is in position 10

The name DOUGLAS is not in the array

 

WORKING WITH RECURSION

(1)  The exclamation point symbol (!) is used in mathematics and is given the name factorial. It is defined as follows: n! = n*(n-1)*(n-2) … 3*2*1. For example, 5! = 5*4*3*2*1=120. Write a recursive method that will compute and return the factorial of any integer.

 

(2)  Recall the algorithm for find the greatest common divisor (GCD) of two integers x and y:

Let r = x % y

  1. If r=0, then GCD = y
  2. If r!=0, then do the following:
    1. Let x=y
    2. Let y=r
    3. Got back to step 1

You originally did this problem when we studied loops. Now write a program that uses a recursive method to find the GCD of two integers input by the user. Display the GCD when it is found. Try a variety of values to test your program.

 

(3)  Let’s now take a look at another recursive function to further analyze the stacking concept. Below is the method whatsTheOutput:

public static void whatsTheOutput()
// accepts a char input,terminates when a period is entered
{
     EasyReader keybd = new EasyReader();
     char letter;
     letter = keybd.readChar();
     if (letter != ‘.’)
         whatsTheOutput( );
     System.out.print(letter);
}

a. Assume that the input sequence of letters is H-E-L-L-O-.. What will the output be?

b. Now try it on the computer and see what the true results are. Explain what is happening and compare it to what you thought would happen.

 

(4)  Recall that the digital root of an integer is the sum of its digits. If that sum is not a single digit number, you repeat the process with the sum until you do have a single digit. For example, the digital root of 4726 is 4+7+2+6 = 19 = 1+9 = 10 = 1+0 = 1.The iterative version might be written as follows:

public static int dr(int num)
{
     int temp;
     while (num>9)
     {
         temp = 0;
         while (num!=0)
         {
              temp+=num % 10;
              num = num / 10; // or num /= 10;
         }
         num = temp;
     }
     return(num);
}

Now write a recursive version of function dr on the computer. Test it with the example data given above. NOTE: This is a challenging problem!

 

(5)  Pascal’s Triangle is a famous arrangement of numbers first discovered by the Chinese. The name Pascal, however, comes from the French mathematician/philosopher Blaise Pascal, who did extensive work with this triangle and found that it had many connections to various branches of mathematics, such as probability.

Recall that the triangle appears as follows:

 

1
1 1
1 2 1
1 3 3 1
etc…

In which each of the numbers in the triangle is the sum of the 2 numbers above it. An alternative arrangement of the triangle is as follows:

1
1 1
1 2 1
1 3 3 1
etc…

 

The numbers in the triangle can be generated using the following recursive algorithm:

For row n and column k, the coefficients of Pascal’s Triangle, C(n, k), can be found by

C(n, 0) = 1
C(n, k) = 0, if k > n
C(n, k) = C(n-1, k) + C(n-1, k-1), for n ≥ k > 0

Important Note: The first row is defined as row 0 and the first column is defined as column 0.

  • Your first job is to write a recursive function that will generate the coefficient for any given row and column location. For example, a call of C(2, 1) will return 2, since the coefficient at row 2, column 1 is 2.

  • The second task is to use this function, along with nested loops, to generate and display rows 0 through 9 of Pascal’s Triangle in the format as shown in the alternative arrangement above. Don’t worry if things don’t look quite so “pretty.”

 

 

 

 

 

Home  •  About Me  •  Webgrades  •  Contact Me
© DanShuster.com