QuestionAssignment1
You must submit one c source file and this file after filling the table
1. Write a function to sort any integer array of size using any algorithm.
2. Write a function to implement the Sequential search algorithm.
3. Write a function to implement the Binary search algorithm.
In main use rand function to generate random numbers from 0 to 9, then test your program in the
worst case by searching for a value greater than 9. Fill a table as follows:
n | Time for Sequential search | Time for sorting | Time for Binary search
---|---|---|---
10 | | |
100 | | |
1000 | | |
10000 | | |
100000 | | |
Hint: to learn how to measure execution time in microseconds, follow the link below
Studdy Solution
STEP 1
1. You need to implement three functions: sorting, sequential search, and binary search.
2. The program should generate random numbers between 0 and 9.
3. The search should be tested for a value greater than 9, ensuring a worst-case scenario.
STEP 2
1. Implement a sorting function.
2. Implement a sequential search function.
3. Implement a binary search function.
4. Generate random numbers and measure execution time.
5. Fill the table with execution times.
STEP 3
Implement a sorting function: - Choose a sorting algorithm (e.g., quicksort, mergesort). - Write a function that takes an array and its size as input and sorts the array.
STEP 4
Implement a sequential search function: - Write a function that takes an array, its size, and a target value as input. - Iterate through the array to find the target value. - Return the index if found, or -1 if not found.
STEP 5
Implement a binary search function: - Ensure the array is sorted before performing a binary search. - Write a function that takes a sorted array, its size, and a target value as input. - Use the binary search algorithm to find the target value. - Return the index if found, or -1 if not found.
STEP 6
Generate random numbers and measure execution time: - Use the `rand` function to generate `n` random numbers for each specified size (10, 100, 1000, 10000, 100000). - Measure the time taken for sorting, sequential search, and binary search. - Use a timing function (e.g., `clock()` in C) to measure execution time in microseconds.
STEP 7
Fill the table with execution times: - Record the measured times for each `n` in the table under the respective columns for sequential search, sorting, and binary search.
Was this helpful?