# hw2 - hw2.cpp Defines the entry point for the console

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

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

View Full Document
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

## This note was uploaded on 04/24/2010 for the course COS 221 taught by Professor Christozov during the Spring '10 term at American University in Bulgaria.

### 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
Ask a homework question - tutors are online