I need help with this code:
#include <iostream>
#include <cstdlib>
#include <string>
using namespace std;
void dump(int ar[], int size)
{
cout << "\nDUMP [ ";
for (int i=0; i<=size; i++)
{
cout << ar[i] << " ";
}
cout << "]\n\n";
}
void quicksort(int ar[], int start, int end)
{
cout << "TOP OF SORT ===========================" <<
endl;
dump(ar, start, end);
int pivot = ar[end];
int left = start;
int right = end - 1;
int tmp;
cout << "Pivot = " << pivot << endl;
while(left < right)
{
cout << "looping from left (starting at " << left
<< ") and from right (starting at index " << right
<< ")\n";
while(ar[left] <= pivot && left < end) left++;
while(ar[right] > pivot && right > start)
right--;
cout << "looping done. left is now " << left <<
", right is now " << right << endl;
if(left >= right) break;
cout << "swapping the " << ar[left] << " (at
index " << left << ") and the "
<< ar[right] << " (at index " << right <<
")." << endl;
tmp = ar[left];
ar[left] = ar[right];
ar[right] = tmp;
left++;
right--;
dump(ar, start, end);
}
cout << "AFTER loop (left and right have crossed!) left = "
<< left << " right = " << right <<
endl;
if(ar[left] > pivot)
{
cout << "swapping the " << ar[left] << " (at
index " << left << ") and the pivot "
<< ar[end] << " (at index " << end << ")."
<< endl; cout << "(note that the pivot is now in its
correct position)\n";
tmp = ar[left];
ar[left] = pivot;
ar[end] = tmp;
}
dump(ar, start, end);
if(left - 1 - start > 0)
{
cout << "calling sort with LESS THAN list:" <<
endl;
quicksort(ar, start, left - 1);
}
if(end - 1 - left > 0)
{
cout << "calling quicksort with GREATER THAN list:" <<
endl;
quicksort(ar, left + 1, end);
}
}
void sort(int list[], int length)
{
quicksort(list, 0, length - 1);
}
int main(void)
{
int ar[100];
int i, v, len;
for (i=0; i<100; i++) {
cout << "enter a number (-1 to quit): ";
cin >> v;
if (v < 0) break;
ar[i] = v;
}
len = i;
cout << "main: before sort:\n";
for (i=0; i<len; i++) {
cout << "main: ar[" << i << "] = " << ar[i]
<< endl;
}
sort(ar, len);
cout << "main: after sort:\n";
for (i=0; i<len; i++) {
cout << "main: ar[" << i << "] = " << ar[i]
<< endl;
}
}
This is what it should be the output:
Please find the code below:
I have updated the dump method in existing code to get the output as required.
#include <iostream>
#include <cstdlib>
#include <string>
using namespace std;
/*
void dump(int ar[], int size){
cout << "\nDUMP [ ";
for (int i=0; i<=size; i++){
cout << ar[i] << " ";
}
cout << "]\n\n";
}
*/
void dump(int ar[], int start,int end){
cout << "\nDUMP [ ";
for (int i=start; i<=end; i++){
cout << ar[i] << " ";
}
cout << "]\n\n";
}
void quicksort(int ar[], int start, int end){
cout << "TOP OF SORT ===========================" <<
endl;
dump(ar, start, end);
int pivot = ar[end];
int left = start;
int right = end - 1;
int tmp;
cout << "Pivot = " << pivot << endl;
while(left < right){
cout << "looping from left (starting at " << left
<< ") and from right (starting at index " << right
<< ")\n";
while(ar[left] <= pivot && left < end) left++;
while(ar[right] > pivot && right > start)
right--;
cout << "looping done. left is now " << left <<
", right is now " << right << endl;
if(left >= right) break;
cout << "swapping the " << ar[left] << " (at
index " << left << ") and the " << ar[right]
<< " (at index " << right << ")." <<
endl;
tmp = ar[left];
ar[left] = ar[right];
ar[right] = tmp;
left++;
right--;
dump(ar, start, end);
}
cout << "AFTER loop (left and right have crossed!) left = "
<< left << " right = " << right <<
endl;
if(ar[left] > pivot){
cout << "swapping the " << ar[left] << " (at
index " << left << ") and the pivot " << ar[end]
<< " (at index " << end << ")." << endl;
cout << "(note that the pivot is now in its correct
position)\n";
tmp = ar[left];
ar[left] = pivot;
ar[end] = tmp;
}
dump(ar,start,end);
if(left - 1 - start > 0){
cout << "calling sort with LESS THAN list:" <<
endl;
quicksort(ar, start, left - 1);
}
if(end - 1 - left > 0){
cout << "calling quicksort with GREATER THAN list:" <<
endl;
quicksort(ar, left + 1, end);
}
}
void sort(int list[], int length){
quicksort(list, 0, length - 1);
}
int main(void){
int ar[100];
int i, v, len;
for (i=0; i<100; i++) {
cout << "enter a number (-1 to quit): ";
cin >> v;
if (v < 0) break;
ar[i] = v;
}
len = i;
cout << "main: before sort:\n";
for (i=0; i<len; i++) {
cout << "main: ar[" << i << "] = " << ar[i]
<< endl;
}
sort(ar, len);
cout << "main: after sort:\n";
for (i=0; i<len; i++) {
cout << "main: ar[" << i << "] = " << ar[i]
<< endl;
}
}
Output:
I need help with this code: #include <iostream> #include <cstdlib> #include <string> using namespace std; void...
Can someone help me with this please: #include <iostream> #include <cstdlib> #include <string> #include <cstring> using namespace std; #define ARSIZ 150000 int debug=0; void dump(int ar[], int len) { for(int i=0; i<len; i++) { cout<<" DUMP: data = : "<< ar[i] << endl; } } void sort(int *ar, int length) { if(length == 1 || length == 0) return; cout << "BREAKING DOWN: " << endl; //for(int i=0; i<length; i++) // cout << " DUMP: DATA = : " <<...
Merge Sort: Time Complexity: O(n log(n)) #include "stdafx.h" #include <iostream> #include <time.h> #include <stdlib.h> using namespace std; void combine(int *a, int low, int high, int mid) { int i, j, k, c[100000]; i = low; k = low; j = mid + 1; while (i <= mid && j <= high) { if (a[i] < a[j]) { c[k] = a[i]; k++; i++; } else { ...
Rewrite this code in Java please #include <iostream> #include <fstream> #include <cstdlib> #include <ctime> using namespace std; long length = 1000; const long max_length = 100000; int list[max_length]; void read() { ifstream fin("random.dat", ios::binary); for (long i = 0; i < length; i++) { fin.read((char*)&list[i], sizeof(int)); } fin.close(); } void bubbleSort() { int temp; for(long i = 0; i < length; i++) { for(long j = 0; j< length-i-1; j++)...
***************Fix code recursive function #include <iostream> #include <cctype> #include <string> using namespace std; void printUsageInfo(string executableName) { cout << "Usage: " << executableName << " [-c] [-s] string ... " << endl; cout << " -c: turn on case sensitivity" << endl; cout << " -s: turn off ignoring spaces" << endl; exit(1); //prints program usage message in case no strings were found at command line } string tolower(string str) { for(unsigned int i = 0; i < str.length(); i++)...
#include<iostream> #include<string> #include<iomanip> using namespace std; /* ********* Class Car ************* ********************************* */ class Car { private: string reportingMark; int carNumber; string kind; bool loaded; string choice; string destination; public: Car() { reportingMark = ""; carNumber = 0; kind = "Others"; loaded = 0; destination = "NONE"; } ~Car() { } void setUpCar(string &reportingMark, int &carNumber, string &kind, bool &loaded, string &destination); }; void input(string &reportingMark, int &carNumber, string &kind, bool &loaded,string choice, string &destination); void output(string &reportingMark, int &carNumber,...
I need to update this code: #include <iostream> #include <string> #include <cctype> using namespace std; int main() { string s; cout<< "Enter a string" <<endl; getline (cin,s); cout<< s <<endl; int vowels=0,consonants=0,digits=0,specialChar=0; for (int i=0; i<s.length(); i++) { char ch=s[i]; if (isalpha(s[i])!= 0){ s[i]= toupper(s[i]); if (ch == 'a'|| ch == 'e'|| ch == 'i'|| ch == 'o' || ch == 'u') vowels++; else consonants++; } else if (isdigit(s[i])!= 0) digits++; else specialChar++; } cout<<"Vowels="<<vowels<<endl; cout<<"Consonants="<<consonants<<endl; cout<<"Digits="<<digits<<endl; cout<<"Special Characters="<<specialChar<<endl;...
I want to compare the runtimes and swap operations times among quick Sort, selection Sort and shell Sort here is my code: But when I create a 1000,000 size array, I can't get the result of the operations times and runtime. what's wrong with my code? and I also want to copy the array. Because I want to use same array for three sort. And for the shell Sort, I haven't learn it in my class. Can anyone help me...
Please add a detailed comment for this program. #include<iostream> #include<string> #include<fstream> #include<sstream> #include<cctype> using namespace std; int is_palindrome(string word){ int len = word.size(); for(int i=0; i<len/2; i++){ if(toupper(word[i])!=toupper(word[len-i-1])) return 0; } return 1; } int have_vowels3(string word){ int cnt = 0; for(int i=0; i<word.size(); i++){ if(tolower(word[i])=='a' || tolower(word[i])=='e' || tolower(word[i])=='i' || tolower(word[i]) =='o' || tolower(word[i]) == 'u') cnt++; } if(cnt>=3) return 1; else return 0; } int have_consecutives(string word){ for(int i=0; i<word.size()-1; i++){ if(tolower(word[i])=='o' && tolower(word[i+1]=='o')) return 1; } return...
Explain the output of the following C++ program. #include <iostream> using namespace std; void Magic(int i=1, int j=2,int k=3, double product =1.0) { i+=2; j*=2; k/=2; product=i*j*k; } void Magic(int& i, int& j, double& product) { i+=2; j=j*2+2; product=i*j; } void Magic(int* i,int* j) { double product; *i+=2; *j=*j*2+2; product=*i * *j; } int main() { double product; int i=0,j=0,k=0; product=i*j*k; Magic(); cout<<"i, j, k and product in main () after 1st round:"<<endl<<i<<endl<<j<<endl<<k<<endl<<product<<endl; Magic(2,4); cout<<"i, j, k and...
#include <iostream> using namespace std; const int SIZE = 10; void displayGreaterThan(int[], int); void displaySmallerThan(int[],int); void displayArrayContent(int[]); void displayLargestValue(int[]); void displaySmallestValue(int[]); int main(){ int number; int numbers[SIZE] = {9,1,90,98,53,22,76,29,37,65}; cout <<"Enter a number: "; cin >> number; cout << endl; displayGreaterThan(numbers,number); cout << endl; displaySmallerThan(numbers,number); cout << endl; displayArrayContent(numbers); cout << endl; displayLargestValue(numbers); cout << endl; displaySmallestValue(numbers); cout << endl; return 0; } void displayGreaterThan(int value[],int num){ cout << " All larger value(s)than" <<...