8:19 cs.sfu.ca dependent, please only include libraries that are part of the C++
ID: 3601666 • Letter: 8
Question
8:19 cs.sfu.ca dependent, please only include libraries that are part of the C++ standard General Requirements Your solution should comply with the following general requirements. Your solution should not be OS dependent, please only include libraries that are part of the C++ standard Specifically, do not include the stdafx.h library since it is Windows specific, if you are using Visual Studio you should uncheck the box that asks if you want to use precompiled headers when you create your project; to remove this option after you've created a project go to Project Properties Configuration Properties-CIC++ Precompiled Headers and select Not Using Precompiled Headers in the appropriate place in the right pane; you should also change your main function to int maino If you are unable to complete a function write a stub for it that returns a default value (if the function has a return value) Make sure that your program compiles and runs before submitting it - we reserve the right to not mark submissions that do not compile, if you are unable to complete the assignment then you should complete what functions you can, write stubs for the remainder and submit a solution that correctly mplements part of the assignment Common Features All your sorting functions should meet the following requirements § Functions should be template functions that take a template variable for the type to be sorted (see the template lab or the read function near the end of this document for examples) § Each sorting function should return an integer equal to the number of barometer operations performed during the function execution; the barometer instruction is the instruction that is executed the greatest number of times; when you count barometer operations choose a single barometer operation for each sort algorithm, you are not intended to be counting the total number of operations performed by the algorithm § The functions should all be written in a single.cpp file that contains your main method; you are not required to write any classes for this assignment assignment Part 1 Insertion Sort § An example of how to call the functions is given at the end of the Write a template function called insertionsort that sorts its array parameter using the insertion sort algorithm. The function should have two parameters, an array of type T (where T is a template variable), and an integer that records the size of the array. The function should return an integer that equals the number of times its barometer operation is made during its execution. Part 2 Quicksort Write a template function called quicksort that sorts its array parameter using the auicksort alaorithm. The function should have two parameters, an array ofExplanation / Answer
************************************************INSERTION SORT**************************************************************
// C program for insertion sort
#include <stdio.h>
#include <math.h>
/* Function to sort an array using insertion sort*/
void insertionSort(int arr[], int n)
{
int i, key, j;
for (i = 1; i < n; i++)
{
key = arr[i];
j = i-1;
/* Move elements of arr[0..i-1], that are
greater than key, to one position ahead
of their current position */
while (j >= 0 && arr[j] > key)
{
arr[j+1] = arr[j];
j = j-1;
}
arr[j+1] = key;
}
}
// A utility function ot print an array of size n
void printArray(int arr[], int n)
{
int i;
for (i=0; i < n; i++)
printf("%d ", arr[i]);
printf(" ");
}
/* Driver program to test insertion sort */
int main()
{
int arr[] = {12, 11, 13, 5, 6};
int n = sizeof(arr)/sizeof(arr[0]);
insertionSort(arr, n);
printArray(arr, n);
return 0;
}
---------------------------------------------------------------------------------------------------------------------------------------------------------
*******************************************QUICK SORT***********************************************************************
/* C implementation QuickSort */
#include<stdio.h>
// A utility function to swap two elements
void swap(int* a, int* b)
{
int t = *a;
*a = *b;
*b = t;
}
/* This function takes last element as pivot, places
the pivot element at its correct position in sorted
array, and places all smaller (smaller than pivot)
to left of pivot and all greater elements to right
of pivot */
int partition (int arr[], int low, int high)
{
int pivot = arr[high]; // pivot
int i = (low - 1); // Index of smaller element
for (int j = low; j <= high- 1; j++)
{
// If current element is smaller than or
// equal to pivot
if (arr[j] <= pivot)
{
i++; // increment index of smaller element
swap(&arr[i], &arr[j]);
}
}
swap(&arr[i + 1], &arr[high]);
return (i + 1);
}
/* The main function that implements QuickSort
arr[] --> Array to be sorted,
low --> Starting index,
high --> Ending index */
void quickSort(int arr[], int low, int high)
{
if (low < high)
{
/* pi is partitioning index, arr[p] is now
at right place */
int pi = partition(arr, low, high);
// Separately sort elements before
// partition and after partition
quickSort(arr, low, pi - 1);
quickSort(arr, pi + 1, high);
}
}
/* Function to print an array */
void printArray(int arr[], int size)
{
int i;
for (i=0; i < size; i++)
printf("%d ", arr[i]);
printf("n");
}
// Driver program to test above functions
int main()
{
int arr[] = {10, 7, 8, 9, 1, 5};
int n = sizeof(arr)/sizeof(arr[0]);
quickSort(arr, 0, n-1);
printf("Sorted array: n");
printArray(arr, n);
return 0;
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.