/* Function Pointer used for bubble sort */ #include "iostream" using namespace std; int compare(int i, int j){ if(i>j) return 1; else return -1; } void bubbleSort(int A[], int n, int (*ptr)(int, int)){ int temp=0; for(int i=0; i<n; i++){ for(int j=0; j<n; j++){ if(ptr(A[j],A[j+1])>0){ temp = A[j]; A[j] = A[j+1]; A[j+1] = temp; } } } } int main(){ int arr[] = {3,1,6,2,4,5}; bubbleSort(arr,6, compare); for(int i=0; i<6; i++) cout<<" "<<arr[i] ; cout<<endl; } /* OUTPUT [pavan@localhost Function-Pointer]$ g++ FPBubbleSort.cpp [pavan@localhost Function-Pointer]$ ./a.out 1 2 3 4 5 6 [pavan@localhost Function-Pointer]$ */
The Tech Zone is built to promote open source technologies. Hadoop, Linux Administration, Cloud, Java Technologies, Operating Systems, Advanced Computer Programming, etc are the key areas of focus. Computer and IT engineering students can find important study material on this portal.
Thursday, 21 September 2017
Function pointer used to do bubble sort in C++
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment