0% found this document useful (0 votes)
53 views

Data - Structure Lab - 03

The document provides instructions for a data structures lab assignment involving sorting, searching, and recursion. It asks students to: 1) Generate and sort random data using insertion sort. 2) Perform binary search on the sorted data. 3) Define functions for input, search, and output. 4) Explain the differences between linear and binary search. 5) Solve recursion problems like printing ranges of numbers, factorials, exponents, and Fibonacci sequences. 6) Attempt a recursive solution for binary search.

Uploaded by

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

Data - Structure Lab - 03

The document provides instructions for a data structures lab assignment involving sorting, searching, and recursion. It asks students to: 1) Generate and sort random data using insertion sort. 2) Perform binary search on the sorted data. 3) Define functions for input, search, and output. 4) Explain the differences between linear and binary search. 5) Solve recursion problems like printing ranges of numbers, factorials, exponents, and Fibonacci sequences. 6) Attempt a recursive solution for binary search.

Uploaded by

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

United International University (UIU)

Dept. of Computer Science and Engineering


Data Structures Laboratory
Course Code: CSI-218
Semester: Summer-2014
Lab – 03

Class assessment: 10 Marks Time 40 Mins

1. WAP that will randomly generate 1000 uppercase letters. Write those alphabets in a text file named
“in.txt”.
2. Sort (in ascending order) all the alphabets in the file “in.txt” using insertion sort. Use separate
function for INSERTION_SORT. Show the sorted output in your console window (Black screen).

Solve following Programming Problems:

[For binary search data needed to be sorted beforehand]

1. WAP that will randomly generate 50000 integer numbers in the range -250 to +249. Write those
numbers in a text file named “in.txt”.

2. Sort (in ascending order) all the integer numbers in the file “in.txt” using insertion sort. Use separate
function for INSERTION_SORT. Call it from main function. Save the sorted output into another text
file named “out.txt”.

[Binary Search]

3. Now do binary search on the data in “out.txt”. Maintain following functions:

a. input // read all integers from “out.txt” and load it into an array
(Do dynamic memory allocation for the array)
b. bin_search // do binary search against a KEY integer and return its index / array position
// if not found then return -1
c. show // Show search result (found or not found) and running time

Function:
int bin_search(int a[ ], int n, int key)
{
int left = 0, right = n-1, mid;

while(1)
{
if(left <= right)
{
mid = left + ((right - left)/2);
if(a[mid] == key)return mid;
else if(key < a[mid])right = mid - 1;
else left = mid + 1;
}
else return -1;
}
} Page 1 of 2
4. What are the difference between linear search and binary search?

[Home task-02]
1. Solve following problems using recursion:
a. Take an input n and print the numbers from 1 to n.
b. Take an input n and print the numbers from n. to 1.
c. Write a recursive function to find out the factorial of input n.
d. Write recursive function to find xn.
e. Write a recursive function that will print nth Fibonacci number.

2. Try to find the recursive solution of binary search?

Page 2 of 2

You might also like