Download presentation
Presentation is loading. Please wait.
Published byMarian Sullivan Modified over 5 years ago
1
Exercise 5 1. We learned bubble sort during class. This problem requires you to modify the code for bubble sorting method to implement the selection sorting function. Selection sorting method finds the smallest element in an array and exchange it with the element in the first position, then find the second smallest element and exchange it with the element in the second position, and continue in this way until the entire array is sorted. Write a program that gets 10 integer numbers from keyboard as an input, and print the numbers in an increasing order using selection sorting method. void selectionSort(int A[], int size) { // put your code here // sort in increasing order } #include <stdio.h> #define N 10 void selectionSort(int A[], int size); int main() { int i; int a[N]; for (i=0;i<N;i++) scanf(“%d”,&a[i]); selectionSort(a,N); for (i=0;i<N;i++) printf(“%d\n”,a[i]); return 0; }
2
2. binary search (recursive function)
/* This function searches for v in array a with size N. If found, it returns the array index. If not found, it returns -1. */ int binsearch(int a[], int N, int v) { // insert your code here } #include <stdio.h> int binsearch(int a[], int N, int v); int main() { int position; int a[5]={1,2,4,5,8}; position = binsearch(a, 5, 2); printf(“%d\n”,position); return 0; }
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.