Using the qsort() and bsearch() functions with values - example program in C
By: Manoj Kumar in C Tutorials on 2007-10-03
This program lets you enter up to MAX values (20 in this case). It sorts the values and prints them in order. Then it lets you enter a value to search for in the array. A printed message states the search's status.
Familiar code is used to obtain the values for the array on lines 18 and 19. Line 26 contains the call to qsort() to sort the array. The first argument is a pointer to the array's first element. This is followed by MAX, the number of elements in the array. The size of the first element is then provided so that qsort() knows the width of each item. The call is finished with the argument for the sort function, intcmp.
The function intcmp() is defined on lines 52 through 55. It returns the difference of the two values passed to it. This might seem too simple at first, but remember what values the comparison function is supposed to return. If the elements are equal, 0 should be returned. If element one is greater than element two, a positive number should be returned. If element one is less than element two, a negative number should be returned. This is exactly what intcmp() does.
The searching is done with bsearch(). Notice that its arguments are virtually the same as those of qsort(). The difference is that the first argument of bsearch() is the key to be searched for. bsearch() returns a pointer to the location of the found key or NULL if the key isn't found. On line 43, ptr is assigned the returned value of bsearch(). ptr is used in the if loop on lines 45 through 48 to print the status of the search.
1: /* Using qsort() and bsearch() with values.*/ 2: 3: #include <stdio.h> 4: #include <stdlib.h> 5: 6: #define MAX 20 7: 8: int intcmp(const void *v1, const void *v2); 9: 10: main() 11: { 12: int arr[MAX], count, key, *ptr; 13: 14: /* Enter some integers from the user. */ 15: 16: printf("Enter %d integer values; press Enter after each.\n", MAX); 17: 18: for (count = 0; count < MAX; count++) 19: scanf("%d", &arr[count]); 20: 21: puts("Press Enter to sort the values."); 22: getc(stdin); 23: 24: /* Sort the array into ascending order. */ 25: 26: qsort(arr, MAX, sizeof(arr[0]), intcmp); 27: 28: /* Display the sorted array. */ 29: 30: for (count = 0; count < MAX; count++) 31: printf("\narr[%d] = %d.", count, arr[count]); 32: 33: puts("\nPress Enter to continue."); 34: getc(stdin); 35: 36: /* Enter a search key. */ 37: 38: printf("Enter a value to search for: "); 39: scanf("%d", &key); 40: 41: /* Perform the search. */ 42: 43: ptr = (int *)bsearch(&key, arr, MAX, sizeof(arr[0]),intcmp); 44: 45: if ( ptr != NULL ) 46: printf("%d found at arr[%d].", key, (ptr - arr)); 47: else 48: printf("%d not found.", key); 49: return(0); 50: } 51: 52: int intcmp(const void *v1, const void *v2) 53: { 54: return (*(int *)v1 - *(int *)v2); 55: } Enter 20 integer values; press Enter after each. 45 12 999 1000 321 123 2300 954 1968 12 2 1999 1776 1812 1456 1 9999 3 76 200 Press Enter to sort the values. arr[0] = 1. arr[1] = 2. arr[2] = 3. arr[3] = 12. arr[4] = 12. arr[5] = 45. arr[6] = 76. arr[7] = 123. arr[8] = 200. arr[9] = 321. arr[10] = 954. arr[11] = 999. arr[12] = 1000. arr[13] = 1456. arr[14] = 1776. arr[15] = 1812. arr[16] = 1968. arr[17] = 1999. arr[18] = 2300. arr[19] = 9999. Press Enter to continue. Enter a value to search for: 1776 1776 found at arr[14]
Add Comment
This policy contains information about your privacy. By posting, you are declaring that you understand this policy:
- Your name, rating, website address, town, country, state and comment will be publicly displayed if entered.
- Aside from the data entered into these form fields, other stored data about your comment will include:
- Your IP address (not displayed)
- The time/date of your submission (displayed)
- Your email address will not be shared. It is collected for only two reasons:
- Administrative purposes, should a need to contact you arise.
- To inform you of new comments, should you subscribe to receive notifications.
- A cookie may be set on your computer. This is used to remember your inputs. It will expire by itself.
This policy is subject to change at any time and without notice.
These terms and conditions contain rules about posting comments. By submitting a comment, you are declaring that you agree with these rules:
- Although the administrator will attempt to moderate comments, it is impossible for every comment to have been moderated at any given time.
- You acknowledge that all comments express the views and opinions of the original author and not those of the administrator.
- You agree not to post any material which is knowingly false, obscene, hateful, threatening, harassing or invasive of a person's privacy.
- The administrator has the right to edit, move or remove any comment for any reason and without notice.
Failure to comply with these rules may result in being banned from submitting further comments.
These terms and conditions are subject to change at any time and without notice.
- Data Science
- Android
- React Native
- AJAX
- ASP.net
- C
- C++
- C#
- Cocoa
- Cloud Computing
- HTML5
- Java
- Javascript
- JSF
- JSP
- J2ME
- Java Beans
- EJB
- JDBC
- Linux
- Mac OS X
- iPhone
- MySQL
- Office 365
- Perl
- PHP
- Python
- Ruby
- VB.net
- Hibernate
- Struts
- SAP
- Trends
- Tech Reviews
- WebServices
- XML
- Certification
- Interview
categories
Related Tutorials
Sum of the elements of an array in C
Printing a simple histogram in C
Find square and square root for a given number in C
Simple arithmetic calculations in C
Passing double value to a function in C
Passing pointer to a function in C
Infix to Prefix And Postfix in C
while, do while and for loops in C
Comments