Searching and Sorting Programs
Searching and Sorting Programs
Lab Manual
CPCS204
Data Structures 1
1435/1436H
Lab - 3
Learning Procedure
Laboratory 3:
Statement Purpose:
This lab will give you the description and implementation some basic Searching
and Sorting Algorithms.
Activity Outcomes:
This lab teaches you the following topics:
Learn some well-known sorting algorithms with their implementations.
Use of Java.util.Arrays class and its under-class methods sort
and binarySearch.
Programming practice on sort methods for sorting array elements.
Instructor Note:
As pre-lab activity, review Ch2 & Ch14, from the book Data Structures with
Java by John R. Hubbard and also the relevant instructor’s slides.
Names I.D.
1. .……………..………………………………. ………………………………
2. ..…………………………………………….. ………………………………
3. .……………………………………………... ………………………………
4. .…………………………………………….. ..…………………………….
1) tage J (Journey)
Sorting Algorithms:
A. Selection Sort
Methodology (Ascending):
B. Bubble Sort
Methodology:
Searching Algorithms:
A. Linear Search
linear search does O(n) comparisons on average.
B. Binary Search
Binary search does O(log n) comparisons at most which is much better than
linear search.
2) Stage a1 (apply)
Example 1:
A static method of the class Arrays for searching the array element in the given
arrays.
Uses the binary search algorithm.
Returns the index of the found element, but if element is not found then returns (-1)
Applicable to arrays of any data type object.
Array used in binarySearch() must be sorted.
The binarsearch() method with Unsorted arrays may give wrong result.
Array having duplicate elements when searched by binarySearch() method may
return any of the duplicate element.
Syntax: public static int binarySearch(Object[] array, Object key)
Task 1: Write a statement to import the library of Arrays to the below program
Task 2: Write and use a linearSearch() method to find index p of key in ar1
import java.util.Arrays;
Example 2:
Built-in JAVA sort()
The sort operation uses a slightly optimized Dual-Pivot Quick sort algorithm
that is stable and faster than traditional One-Pivot counterpart.
Runs in O(n log n)time.
Applicable to List objects consisting numbers or alphabetical content.
Syntax: public static void sort(Object[] a) or
public static void sort(Object[] a, int fromIndex, int toIndex)
Task 1: Use the built-in JAVA sorting method for an array's elements. Besides, printing
array elements at once without loop (i.e. for or while… etc.) (Retype and try this code)
Task 2: Clone (copy) array old to new array Arr then use sort(Object[] a, int
fromIndex, int toIndex) to sort the right-half of array Arr. Print Arr after sort.
import java.util.Arrays;
import java.util.Random;
/**
*/
for(int i=0;i<num.length;i++)
num[i]=rand.nextInt(100);
System.out.println("Old ="+Arrays.toString(old));
System.out.println("num ="+Arrays.toString(num));
//Task2
Arrays.sort(Arr,5,10);
System.out.println("Arr ="+Arrays.toString(Arr));
Potential Output:
3) Stage v (verify)
Programming Exercises
Ex-1:
1. Write a program that reads 10 integer numbers and sort them with 2
different sorting algorithms.
2. Create 2 public classes. One called TestSorting and the other called
Sorting.
3. Sorting class includes above 2 sorting methods (i.e. Selection, and
Bubble) and 1 more method for printing the sorted array.
4. TestSorting class includes the main method, and uses
java.util.scanner to enter 10 integers.
5. Create and object of class Sorting Sort=new Sorting().
6. Then it calls the 2 methods in class Sorting then print the original array
and the 2 sorted arrays. So, the output would be such that:
Please enter a num[0]: 5
Please enter a num[1]: 6
Please enter a num[2]: 9
Please enter a num[3]: 7
Please enter a num[4]: 2
Please enter a num[5]: 4
Please enter a num[6]: 3
Please enter a num[7]: 8
Please enter a num[8]: 10
Please enter a num[9]: 13
salespeople earned salaries in each of the following ranges (assume that each
salesperson's salary is truncated to an integer amount):
$200-299
$300-399
$400-499
$500-599
$600-699
$700-799
$800-899
$900-999
$1000 and over
Summarize the results in tabular format.
4) Stage a2 (assess)
Lab Work:
In each laboratory you are assessed on your work within lab session based on
your participation, discussions and achievement of lab activities. Thus, each lab
has a portion of the (LAB WORK MARK). Therefore, a checklist of each lab is
used to evaluate your work. This checklist accounts the following criteria: