0% found this document useful (0 votes)
33 views13 pages

Lab 08

Uploaded by

mahin ramchagol
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
33 views13 pages

Lab 08

Uploaded by

mahin ramchagol
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 13

Sorting and Searching

Course Code: CSC 2107 Course Title: Data Structure (Lab)

Dept. of Computer Science


Faculty of Science and Technology

Lecturer No: 8 Week No: 8 Semester:


Lecturer: Name & email
Rules and Guidelines

Write your own rules to evaluate lab tasks.


Prerequisites

 Have a clear and full understanding of Sorting and Searching algorithms.

 Theory Lectures 5.1 & 5.2


Objectives

 To implement how to sort data elements stored in an array using different


algorithms.

 To implement how to search a data element in an array using Binary Search

 To compare different sorting and searching algorithms

 To solve more complex problems related to sorting and searching


Lab Tasks

1. Write a C++ code to implement Bubble Sort


2. Write a C++ Program for Binary Search Implementation

**perform time complexity for the two programs


Bubble Sort: Implementation

Do the following to write program for bubble sort:


Store some numbers in an array
Print the unsorted input array
Apply sorting using Bubble Sort algorithm
Print the sorted output array
Bubble Sort: Example

Input:
[92, 82, 21, 16, 18, 95]

Output:
[16, 18, 21, 82, 92, 95]
Number of comparisons = 14
Number of exchanges = 9
Binary Search: Implementation

Do the following to write program for binary search:


Store some numbers in an array
Apply a sorting algorithm to sort the array
Print the sorted input array
Input a searching number
Apply searching using Binary Search algorithm
Print the position if found otherwise print “Not found”
Binary Search: Example 1

Input:
[92, 82, 21, 16, 18, 95, 25, 10]
92

Output:
[10, 16, 18, 21, 25, 82, 92, 95]
92 found at index 6 in the sorted array
Number of comparisons = 2
Binary Search: Example 2

Input:
[92, 82, 21, 16, 18, 95, 25, 10]
30

Output:
[10, 16, 18, 21, 25, 82, 92, 95]
30 not found in the array.
Number of comparisons = 3
Home Tasks

1. Implement selection sort and also print the


number of comparisons and exchanges used.

2. Implement insertion sort and also print the number


of comparisons and shifts used.

In both cases, use the same input used in Bubble sort


to show the different complexities.
References
1. Theory Lectures 5.1 and 5.2

2. https://en.wikipedia.org/wiki/Bubble_sort

3. https://en.wikipedia.org/wiki/Selection_sort

4. https://en.wikipedia.org/wiki/Insertion_sort

5. https://en.wikipedia.org/wiki/Sorting_algorithm

6. https://en.wikipedia.org/wiki/Binary_search_algorithm

7. Nice animations are available here


https://www.cs.usfca.edu/~galles/visualization/ComparisonSort.html
Books
 “Schaum's Outline of Data Structures with C++”. By John R. Hubbard
 “Data Structures and Program Design”, Robert L. Kruse, 3rd Edition, 1996.
 “Data structures, algorithms and performance”, D. Wood, Addison-Wesley, 1993
 “Advanced Data Structures”, Peter Brass, Cambridge University Press, 2008
 “Data Structures and Algorithm Analysis”, Edition 3.2 (C++ Version), Clifford A.
Shaffer, Virginia Tech, Blacksburg, VA 24061 January 2, 2012
 “C++ Data Structures”, Nell Dale and David Teague, Jones and Bartlett Publishers,
2001.
 “Data Structures and Algorithms with Object-Oriented Design Patterns in C++”,
Bruno R. Preiss,

You might also like