hw2 - // hw2.cpp : Defines the entry point for the console...

Info iconThis preview shows pages 1–3. Sign up to view the full content.

View Full Document Right Arrow Icon

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: // hw2.cpp : Defines the entry point for the console application. // / #include "stdafx.h" # #include <iostream> #include <cstdlib> #include <iomanip> # using namespace std; u class comp { public: comp(int b) { l = b; x = new (int[l]); } comp(int * a, int b) {x=a; l = b; counter = 0;} int *x; int l; unsigned long double counter; int length() {return l;}; void increment(){ counter ++;} // increment counter of the iterations' number void reset(){counter = 0;} // reset the counter for the next sorting function void print() { cout << counter << "\n"; } }; } //---------------------------------------------------------------------- // function: bubbleSort //---------------------------------------------------------------------- / template <class T> void bubbleSort(T & data) { int * temp = new int[data.length()]; // a new array is created since for(int j = 0; j < data.length(); j++) // the sorting funtions are given as argument the same array temp[j] = data.x[j]; // sort, in place. the argument vector into ascending order for (int top = data.length() - 1; top > 0; top--) { // find the largest element in position 0..top // place in data[top] for (int i = 0; i < top; i++) { // inv: data[i] is largest element in 0..i if (temp[i+1] < temp[i]) { swap(temp[i+1], temp[i]); } data.increment(); d // inv: data[i+1] is largest element in 0..i+1 } // inv: data[top..length-1] is properly ordered } delete temp; } //---------------------------------------------------------------------- // function: insertionSort //---------------------------------------------------------------------- / template <class T> void insertionSort(T & data) { int * temp = new int[data.length()]; bool swaped; for(int j = 0; j < data.length(); j++) temp[j] = data.x[j]; // sort, in place. the argument vector into ascending order int n = data.length(); for (int top = 1; top < n; top++) { swaped = false;...
View Full Document

Page1 / 6

hw2 - // hw2.cpp : Defines the entry point for the console...

This preview shows document pages 1 - 3. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online