Sorting : Bubble Sort & Selection Sort

 

 

Problem #1:

Write a program that will accept distinct integer values as the input and print the order of the values as either ascending or descending. If the values are neither ascending nor descending, your program should sort and print the values in ascending order.  

 

Examples:

#1:

How many values?

5

Input#1:

1

Input #2:

 2

Input #3:

 3

Input #4:

 4

Input #5:

 5

 

Output:

Ascending order  

 

#2:

How many values?

5

Input#1:

8

Input #2:

7

Input #3:

 5

Input #4:

 3

Input #5:

 1

 

Output:

Descending order  

 

#3:

Input:

How many values?

5

Input#1:

14

Input #2:

3

Input #3:

-8

Input #4:

 2

Input #5:

88

  

Output:

The values were in mixed order.

 

Values sorted in ascending order:

 -8, 2, 3, 4, 14, 88

 

 

Problem #2:

Write a telephone directory program that reads names and phone numbers into two arrays (both of type String) and sorts (modify the bubble sort or the selection sort program to handle strings) both arrays by name (see example below).

 

Example:

 How many entries?

5

 

Enter Name#1: 

Peter

Enter Number for Peter:

234-1234

 

Enter Name#1: 

Paul

Enter Number for Paul:

734-2534

 

Enter Name#1: 

Joe

Enter Number for Joe:

114-1832

 

Enter Name#1: 

Alice

Enter Number for Alice:

100-2012

 

Enter Name#1: 

John

Enter Number for John:

700-5000

 

 

 

Unsorted Phonebook:

Peter : 234-1234

Paul    : 734-2534

Joe    : 114-1832

Alice  100-2012

John : 700-5000

 

 

Sorted Phonebook:

Alice   100-2012

Joe    : 114-1832

John : 700-5000

Paul    : 734-2534

Peter  : 234-1234

 

 

 

Problem #3:  Write a program to implement the following:

a)    Ask the user for an input size (integer type) of the array

b)   Declare a string array of the input size.

c)     Take input values (words) for the string array.

d)   Using bubble sort or selection sort, sort the words in the string array in increasing order of their lengths.

 

Example (Using Bubble Sort):

Input the size of the array

5

Enter Value#1:

because

Enter Value#2:

abe

Enter Value#3:

lincoln

Enter Value#4:

was

Enter Value#5:

a

 

Initial Array:  because  abe  lincoln  was  a 

 

After Pass#1 :  abe  because  was  a  lincoln 

 

After Pass#2 :  abe  was  a  because  lincoln 

 

After Pass#3 : abe  a  was  because  lincoln 

 

After Pass#4 : a  abe  was  because  lincoln 

 

Total Number of Passes = 4

 

 

Final Array:  a  abe  was  because  lincoln