Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include<iostream>
- using namespace std;
- int isMonotoneIncreasing(int numbers[], int count)
- {
- if (count == 0) return 1;
- int i = 0;
- while(i < count - 1 &&
- numbers[i] <= numbers[i+1])
- {
- i += 1;
- }
- return i == count - 1;
- }
- int scalarProduct(int a[], int b[], int count)
- {
- int sum = 0;
- for(int i = 0; i < count; ++i)
- {
- sum += a[i] * b[i];
- }
- return sum;
- }
- int longestNonDecreasingLength(int numbers[], int arrayLength)
- {
- int maxCount = 1;
- int currentCount = 1;
- for(int i = 0; i < arrayLength - 1; ++i)
- {
- if(numbers[i] <= numbers[i+1]) {
- currentCount += 1;
- maxCount = max(maxCount, currentCount);
- }
- else {
- currentCount = 0;
- }
- }
- currentCount += 1;
- maxCount = max(maxCount, currentCount);
- return maxCount;
- }
- bool isSubset(int a[], int countA, int b[], int countB)
- {
- for(int i = 0; i < countA; ++i)
- {
- bool found = false;
- for(int j = 0; j < countB; ++j)
- {
- if (a[i] == b[j])
- {
- found = true;
- break;
- }
- }
- if (!found)
- return false;
- }
- return true;
- }
- int sameElements(int a[], int countA, int b[], int countB)
- {
- return isSubset(a, countA, b, countB) &&
- isSubset(b, countB, a, countA);
- }
- int subSequenceCount(int sequence[], int sequenceCount,
- int subSequence[], int subSequenceCount)
- {
- if (subSequenceCount > sequenceCount)
- return 0;
- int count = 0;
- int j = 0;
- for(int i = 0; i < sequenceCount - subSequenceCount; ++i)
- {
- for(int j = 0; j < subSequenceCount; ++j)
- {
- if (sequence[i+j] == subSequence[j])
- {
- if (j == subSequenceCount - 1)
- {
- count += 1;
- }
- }
- else
- {
- j = subSequenceCount;
- }
- }
- }
- return count;
- }
- void bubbleSort(int a[], int count)
- {
- int k = 0;
- int pass = 0;
- while(count > 1)
- {
- k = 0;
- for(int i = 0; i < count - 1; ++i)
- {
- if(a[i] > a[i+1])
- {
- swap(a[i], a[i+1]);
- k=i;
- }
- }
- count = k + 1;
- }
- }
- int binSearch(int a[], int count, int elem)
- {
- if (count == 0)
- return -1;
- int left = -1;
- int right = count;
- int middle = 0;
- while(right - left > 1 &&
- a[middle] != elem)
- {
- middle = (left + right) / 2;
- if (a[middle] > elem) right = middle;
- else left = middle;
- }
- if (a[middle] == elem)
- return middle;
- return -1;
- int l=0,r=count-1;
- while(l<=r)
- {
- middle=(l+r)/2;
- if (elem<a[middle]) r=middle-1;
- else if (elem>a[middle]) l=middle+1;
- else if (elem==a[middle]) return 1;
- else return -1;
- }
- }
- void merge(int a[],
- int left, int countLeft,
- int right, int countRight,
- int buf[])
- {
- int bufIndex = 0;
- int leftStart = left;
- while(true)
- {
- if (countLeft <= 0)
- {
- if (countRight <= 0)
- break;
- buf[bufIndex] = a[right];
- right++;
- bufIndex++;
- countRight--;
- }
- else if (countRight <= 0)
- {
- buf[bufIndex] = a[left];
- left++;
- bufIndex++;
- countLeft--;
- }
- else if (a[left] < a[right])
- {
- buf[bufIndex] = a[left];
- left++;
- bufIndex++;
- countLeft--;
- }
- else
- {
- buf[bufIndex] = a[right];
- right++;
- bufIndex++;
- countRight--;
- }
- }
- for(int i = 0; i < bufIndex; ++i)
- {
- a[leftStart+i] = buf[i];
- }
- }
- void mergeSort(int a[], int start, int count, int buf[])
- {
- if (count <= 1)
- return;
- mergeSort(a, start, count / 2, buf);
- mergeSort(a, start + count/2, count/2 + count%2, buf);
- merge(a, start, count/2, start+count/2, count/2+ count%2, buf);
- }
- int main()
- {
- int a[] = {1, 2, 3, 4, 5, 6};
- int b[] = {1, 4, 3, 4, 5, 6, 2};
- int k[] = {1,2,1,3,2,1};
- int l[] = {2,1};
- int binSearchArray[] = {1,1,2,2,3,4,7,8,9,9,10};
- int aa[] = {'a','a'};
- int aaaaa[] = {'a','a','a','a','a'};
- cout<<"isMonotoneIncreasing a: "<<isMonotoneIncreasing(a, 6)<<endl;
- cout<<"isMonotoneIncreasing b: "<<isMonotoneIncreasing(b, 6)<<endl;
- cout<<"scalarProduct a*b: "<<scalarProduct(a, b, 6)<<endl;
- cout<<"longestNonDecreasing a,b: "<<longestNonDecreasingLength(b, 7)<<endl;
- cout<<"sameElements a,b: "<<sameElements(a, 6, b, 7)<<endl;
- cout<<"subSequenceCount [1,2,1,3,2,1],[2,1]: "<<
- subSequenceCount(k, 6, l, 2)<<endl;
- cout<<"subSequenceCount [a,a,a,a,a],[a,a]: "<<
- subSequenceCount(aaaaa, 5, aa, 2)<<endl;
- bubbleSort(k, 6);
- cout<<"bubbleSort of [1,2,1,3,2,1]: ";
- for(int i = 0; i < 6; ++i)
- cout<<k[i]<<" ";
- cout<<endl;
- cout<<"binSearch of 2 in [1,1,2,2,3,4,7,8,9,9,10]: "<<binSearch(binSearchArray, 11, 2)<<endl;
- cout<<"binSearch of 4 in [1,1,2,2,3,4,7,8,9,9,10]: "<<binSearch(binSearchArray, 11, 4)<<endl;
- cout<<"binSearch of 5 in [1,1,2,2,3,4,7,8,9,9,10]: "<<binSearch(binSearchArray, 11, 5)<<endl;
- int mergeSortArray[] = {1, 5, 2, 3, 6, 1, 3, 2, 9 };
- int mergeSortBuf[9];
- mergeSort(mergeSortArray, 0, 9, mergeSortBuf);
- cout<<"mergeSort of of 5 in [1, 5, 2, 3, 6, 1, 3, 2, 9]: "<<endl;
- for(int i = 0; i < 9; ++i)
- cout<<mergeSortArray[i]<<" ";
- cout<<endl;
- //std::vector<double> kAsVector = new std::vector
- }
Advertisement
Add Comment
Please, Sign In to add comment