int main() { Sorting sortClass; int sortMe[LIST_SIZE] = { 15, 14, 16, 7, 4, 8, 1 }; // INITIAL ARRAY sortClass.LoadArray(sortMe); cout << "THE INITIAL LIST CONTAINS" << endl; sortClass.DisplayArray(); cout << endl; // BUBBLE SORT sortClass.LoadArray(sortMe); cout << "BEGIN BUBBLE SORT" << endl; sortClass.BubbleSort(); sortClass.DisplayArray(); // INSERTION SORT sortClass.LoadArray(sortMe); cout << "BEGIN INSERTION SORT" << endl; sortClass.InsertionSort(); sortClass.DisplayArray(); // SELECTION SORT sortClass.LoadArray(sortMe); cout << "BEGIN SELECTION SORT" << endl; sortClass.SelectionSort(); sortClass.DisplayArray(); return 0; }
//closest to zero sum void minAbsSumPair(int A[], int n) { /* Array should have at least two elements*/ if(n < 2) { cout<<"Invalid Input"; return; } Sorting *obj = &Sorting(); obj->SetArray(A, n); obj->InsertionSort(); // left and right index variables int l = 0, r = n-1; int sum,minsum=A[r]; int min_l,min_r; while(l<r) { sum=A[l]+A[r]; if(abs (minsum) > abs(sum)) { minsum=sum; min_l = l; min_r = r; } if(sum >= 0) r--; else l++; } cout<<"min sum is "<<A[min_l]<< " + " <<A[min_r]<<" = "<< minsum; }
bool CheckDuplicacy(int *Arr,int len) { Sorting *obj = &Sorting(); obj->SetArray(Arr, len); obj->InsertionSort(); for (int i=0;i<len;i++) { if (Arr[i]=Arr[i+1]) return true; } return false; }
int hasArrayTwoCandidates(int A[], int size, int k) { Sorting *obj = &Sorting(); obj->SetArray(A, size); obj->InsertionSort(); int left=0; int right=size-1; while(left<right) { if (A[left]+A[right] == k ) { cout<<"number are "<<A[left] <<" ,"<<A[right]<<endl; return 1; } if ( A[left]+A[right] < k) left++; else right-- ; } return 0; }
int MaxOccurance(int *Arr,int len) { int currentcount=0,maxc=0; Sorting *obj = &Sorting(); obj->SetArray(Arr, len); obj->InsertionSort(); int currentcandidate=Arr[0],maxcandidate = Arr[0]; for(int i=0;i<len;i++) { if(Arr[i]==currentcandidate) currentcount++; else { currentcandidate=Arr[i]; currentcount=1; } if(currentcount>maxc) { maxc=currentcount; maxcandidate=currentcandidate; } } return maxcandidate; }