## A problem which i've been doing for hours

Discuss all kind of algorithms and data structures from their mathematical and programming sides.

Moderators: Darobat, RecursiveS, Dante Shamest, Bugdude, Wizard

### A problem which i've been doing for hours

dear profesional over here...please help me with this problem,i have been trying to solve this problem for pass few hours..and i am still a beginner but the task given to me was too tough for me..

i want to improve..

--Create several files of integers to be used to test sorting methods. Make files of several sizes, for example, size 20, 200, and 2000. Make files that are in order, in reverse order, in random order, and partially in order. By keeping all this test data in files (rather than generating it with random numbers each time the program is run), the same data can be used to test different sorting methods, and hence it will be easier to compare their performance.

--Write a menu-driven program for testing various sorting methods. One option is to read a file of integers into a list. Other options will be to run one of various sorting methods on the list, to print the unsorted or the sorted list, and to quit. After the list is sorted and (optionally) printed, it should be discarded so that testing a later testing method will start with the same input data. Write the program such that each new sorting method will process the original input file.

--Include code in the program to calculate and print

a. CPU time
b. The number of comparisons of keys.
c. The number of assignments of list entries during sorting a list.

Counting comparisons and assignments requires setting up global variables and inserting code into each sorting procedure to increment one of these global variables each time a key comparison and entry assignment is made.

#include<iostream>

#include<iomanip>

#include <cstdarg>

#include <fstream>

#include <algorithm>

int array[1000];

#include <stdlib.h>

#include<ctime>

#include <math.h>

#include<time.h>

using namespace std;

const int SIZE = 5000;

//function declarations

void selection_sort(int array[], int size);

void insertion_sort(int array[], int size);

int bin_search(int array[], int size, int item_sought);

void displayarray(int array[]);

void main()

{

cout<<"********************************************"<<endl;
cout<<""<<endl;
cout<<"Welcome to the sorting program assignment1"<<endl;
cout<<""<<endl;
cout<<"********************************************"<<endl;
cout<<""<<endl;
cout<<"Please enter your choice of type of sorting that you would like to perform:"<<endl;
cout<<""<<endl;

//initialization of variables

int choice=0;

int sortarray[3]={0};

int item=0;

int i=0;

while (choice !=-1)

{

choice=0;//This is to make sure the loop executes again

while (choice != 1 && choice !=2 && choice !=3)

{

cout << "1 For selection sort"<<endl;
cout<<"2 For insertion sort"<<endl;
cout<<"3 For binary search" << endl;

cin >> choice;

}

cout << "Please enter the values in a row to use for the sort algorithm" << endl;

//input array values

for (i=0;i<2001;i++)

cin >> sortarray[i];

//menu style selector for the user

if (choice==1)

selection_sort(sortarray, 3);

else if (choice==2)

insertion_sort(sortarray, 3);

else if (choice==3)

{

cout << "enter number sort";

cin >> item;

bin_search(sortarray, 3, item);

}

else

cout << "poor choice skippy" << endl;

cout << "Enter -1 to quit, make sure to enter a number, any number, otherwise" << endl;

cin >> choice;

}

}

void selection_sort(int array[], int size)

{

int n = size; // 0

for(int i = 0; i < n - 1; i++) // 1

{// 2

int smallest = array[i]; // 3

int small_pos = i; // 4

for(int j = i + 1; j <= n - 1; j++) // 5

if (array[j] < smallest) // 6

{ // 7

smallest = array[j]; // 8

small_pos = j; // 9

} // 10

array[small_pos] = array[i]; // 11

displayarray(array);

array[i] = smallest; // 12

displayarray(array);

} // 13

}

void insertion_sort(int array[], int size)

{

int next_element;

int n = size; // 0

for(int i = 1; i <= n - 1; i++) // 1

{ // 2

next_element = array[i]; // 3

int j = i; // 4

while(j > 0 && next_element < array[j-1])// 5

{

array[j] = array[j-1];

j--;

displayarray(array);

}

array[j] = next_element;

displayarray(array);

}

}

int bin_search(int array[], int size, int item_sought)

{

int n = size;

int first = 0;

int middle;

int last = n - 1;

bool found = false;

while(first <= last && !found)

{

middle = (first + last) / 2;

if(item_sought < array[middle])

last = middle - 1;

else if(item_sought > array[middle])

first = middle + 1;

else

found = true;

displayarray(array);

}

if(found)

return middle;

else

return n;

}

void displayarray(int array[])

{

//This function displays the values of the array

for (int i=0;i<2001;i++)

cout << setw(2) << array[i];

cout << endl;

}
snoop82

Posts: 6
Joined: Wed Sep 19, 2007 1:48 am

Stop opening new threads for the same issue. It's annoying. Do your own homework. If you get stuck because there is something that you don't know how to implement, ask a specific question.

Alvaro
Moderator

Posts: 5185
Joined: Mon Sep 22, 2003 4:57 pm
Location: NY, USA

Closed in favor of the other thread(s?)
Need information on a function I've posted? Chances are it's at the MSDN.
MXP

Posts: 6506
Joined: Mon Sep 22, 2003 5:27 pm