Question

In: Computer Science

Do an insertion sort based off this code #include <iostream> #define MAX_INT 2147483647 using namespace std;...

Do an insertion sort based off this code

#include <iostream>

#define MAX_INT 2147483647

using namespace std;
  

int main(int argc,char **argv) {

int* Sequence;
int arraySize = 1;

// Get the size of the sequence
cin >> arraySize;
Sequence = new int[arraySize];
  
// Read the sequence
for(int i=0; i<arraySize; i++)
cin >> Sequence[i];

Solutions

Expert Solution

Insertion sort works by comparing the elements from second element in the array with all the elements below it and move that element such that up to that part the array remains sorted.

Insertion Sort Code:

#include <iostream>

#define MAX_INT 2147483647

using namespace std;
  

int main(int argc,char **argv) {

int* Sequence;
int arraySize = 1;
int i,j,pivot;

// Get the size of the sequence
cin >> arraySize;
Sequence = new int[arraySize];
  
// Read the sequence
for(int i=0; i<arraySize; i++)
cin >> Sequence[i];

for(i=1;i<arraySize;i++){
j=i-1;
pivot = Sequence[i];
  
//Comparing the elements from second element with all the elements below it
//and made the array sorted upto that point
while(j>=0 && Sequence[j] > pivot){
Sequence[j+1]=Sequence[j];
j=j-1;
}
  
Sequence[j+1]=pivot;
}
cout<<"The sorted array is:\n";
for(int i=0; i<arraySize; i++)
cout << Sequence[i]<<" ";

}

Output:


Related Solutions

implement heap sort algorithm continuing off this #include <iostream> #define MAX_INT 2147483647 using namespace std; int...
implement heap sort algorithm continuing off this #include <iostream> #define MAX_INT 2147483647 using namespace std; int main(int argc,char **argv) { int* array; int arraySize = 1; // Get the size of the sequence cin >> arraySize; array = new int[arraySize]; // Read the sequence for(int i=0; i<arraySize; i++){ cin >> array[i]; }
--- TURN this Code into Java Language --- #include <iostream> #include <string> using namespace std; //...
--- TURN this Code into Java Language --- #include <iostream> #include <string> using namespace std; // constants const int FINAL_POSITION = 43; const int INITIAL_POSITION = -1; const int NUM_PLAYERS = 2; const string BLUE = "BLUE"; const string GREEN = "GREEN"; const string ORANGE = "ORANGE"; const string PURPLE = "PURPLE"; const string RED = "RED"; const string YELLOW = "YELLOW"; const string COLORS [] = {BLUE, GREEN, ORANGE, PURPLE, RED, YELLOW}; const int NUM_COLORS = 6; // names...
write the algorithm for this the code?!. #include<iostream> using namespace std; #include<string.h> int main() { char...
write the algorithm for this the code?!. #include<iostream> using namespace std; #include<string.h> int main() { char plain[50], cipher[50]="", decrypt[50]=""; int subkeys[50], len;       cout<<"Enter the plain text:"<<endl; cin>>plain;    cout<<"Enter the first subkey:"<<endl; cin>>subkeys[0];    _strupr(plain);    len = strlen(plain);    /**********Find the subkeys**************/    for(int i=1; i<len; i++) { if ((plain[i-1]>='A') && (plain[i-1]<='Z')) { subkeys[i] = plain[i-1]-65; } }    /****************ENCRYPTION***************/       for(int i=0; i<len; i++) { if ((plain[i]>='A') && (plain[i]<='Z')) {    cipher[i] = (((plain[i]-65)+subkeys[i])%26)+65; }...
Complete the C++ code #include <iostream> #include <stdlib.h> #include <time.h> using namespace std; struct Cell {...
Complete the C++ code #include <iostream> #include <stdlib.h> #include <time.h> using namespace std; struct Cell { int val; Cell *next; }; int main() { int MAX = 10; Cell *c = NULL; Cell *HEAD = NULL; srand (time(NULL)); for (int i=0; i<MAX; i++) { // Use dynamic memory allocation to create a new Cell then initialize the // cell value (val) to rand(). Set the next pointer to the HEAD and // then update HEAD. } print_cells(HEAD); }
I need the following code completed: #include <iostream> #include <time.h> #include "CSVparser.hpp" using namespace std; //============================================================================...
I need the following code completed: #include <iostream> #include <time.h> #include "CSVparser.hpp" using namespace std; //============================================================================ // Global definitions visible to all methods and classes //============================================================================ // forward declarations double strToDouble(string str, char ch); // define a structure to hold bid information struct Bid { string bidId; // unique identifier string title; string fund; double amount; Bid() { amount = 0.0; } }; // FIXME (1): Internal structure for tree node struct Node {    Bid data; Node *left; Node...
Implement the following functions for an array based stack. #include <stdexcept> #include <iostream> using namespace std;...
Implement the following functions for an array based stack. #include <stdexcept> #include <iostream> using namespace std; #include "Stack.h" template <typename DataType> class StackArray : public Stack<DataType> { public: StackArray(int maxNumber = Stack<DataType>::MAX_STACK_SIZE); StackArray(const StackArray& other); StackArray& operator=(const StackArray& other); ~StackArray(); void push(const DataType& newDataItem) throw (logic_error); DataType pop() throw (logic_error); void clear(); bool isEmpty() const; bool isFull() const; void showStructure() const; private: int maxSize; int top; DataType* dataItems; }; //-------------------------------------------------------------------- // // // ** Array implementation of the Stack ADT...
Complete the code provided to add the appropriate amount to totalDeposit. #include <iostream> using namespace std;...
Complete the code provided to add the appropriate amount to totalDeposit. #include <iostream> using namespace std; int main() { enum AcceptedCoins {ADD_QUARTER, ADD_DIME, ADD_NICKEL, ADD_UNKNOWN}; AcceptedCoins amountDeposited = ADD_UNKNOWN; int totalDeposit = 0; int usrInput = 0; cout << "Add coin: 0 (add 25), 1 (add 10), 2 (add 5). "; cin >> usrInput; if (usrInput == ADD_QUARTER) { totalDeposit = totalDeposit + 25; } /* Your solution goes here */ else { cout << "Invalid coin selection." << endl;...
Question 3 A. Show the output of the following code. #include <iostream> using namespace std; void...
Question 3 A. Show the output of the following code. #include <iostream> using namespace std; void magic (int &a, int b, int& c) {   a *= 2;    b = b+2;    c = c-2; } int main () { int x=1, y=3, z=7;    magic (x, y, z); cout << "x=" << x << ", y=" << y << ", z=" << z;    magic (z, y, x); cout << "x=" << x << ", y=" << y <<...
In C++, assuming you have the following incomplete code: #include<iostream> #include <unistd.h> using namespace std; //...
In C++, assuming you have the following incomplete code: #include<iostream> #include <unistd.h> using namespace std; // Structure for storing the process data struct procData { char pname[5]; // Name of a process int arrivt; //Arrival time of a process int pburst; // Burst time of a process int endtime; // Exit time/ Leaving time of a process int remburst; // Remaining burst time of a process int readyFlag; // boolean, Flag for maintaining the process status }; // Global variable...
C++ Given Code: #include <iostream> #include <string> using namespace std; int main() { //declare variables to...
C++ Given Code: #include <iostream> #include <string> using namespace std; int main() { //declare variables to store user input bool cont = true; //implement a loop so that it will continue asking until the user provides a positive integer // the following provides ONLY part of the loop body, which you should complete { cout <<"How many words are in your message? \n"; cout <<"Enter value: "; // get user input integer here    cout <<"\nInvalid value. Please Re-enter a...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT