# Thread: Bubble Sort / selection problem

1. ## Bubble Sort / selection problem

Supose you have a group of N numbers and would like to determine the kth largest.
One way to solve this problem would to read N numbers into an array, sort them in
decreasing order by some simple algorithm such as bubblesort, and then return the
element in position.
Here is what the problem is asking for
Write a program to solve this problem. Let k = N/2. Draw a table showing the running time of your program for various values of N.
Can some one get me started on the code for this problem or explain it better to me.
Input file: 300 5000 300000 200000 30 45 70 500000 3000

Code:
```#include< iostream >
#include< fstream >

const int MAXVALUES = 9;
using namespace std;

int main()
{

//Declare the streams and the input and output files
ifstream inFile;
ofstream outFile;

inFile.open("A:/input1.txt");
outFile.open("A:/output1.txt");

int i = 0;
int j = 0;
int temp = 0;
int sortValue[MAXVALUES];

for(i=0; i<MAXVALUES; i++)
{
inFile>> sortValue[i];

}

inFile.close();
for(i=0;i<MAXVALUES-1;++i)
{
for(j=0;j<MAXVALUES-1;j++)
{
if(sortValue[j+1] > sortValue[j])
{
temp = sortValue[j];
sortValue[j] = sortValue[j+1];
sortValue[j+1] = temp;
}
}
}

for(i=0;i<MAXVALUES;i++)
outFile<< "Array[" << i << "] : " << sortValue[i] << endl;

return 0;
}```

2. Sounds to me like you would want to divide the number of elements in the array by two and then display that element, ie:
Code:
```int array[N]=//some values
//if the number of elements is known
int elemCnt = (N/2); //N/2=k
//sort the elements
...
//output the kth highest number
cout<<array[elemCnt];```
That would be my guess

3. ## so I would do this

So in my code I would change this line int sortValue[MAXVALUES]
to this int sortValue[MAXVALUES/2]?

Code:
```#include< iostream >
#include< fstream >

const int MAXVALUES = 9;
using namespace std;

int main()
{

//Declare the streams and the input and output files
ifstream inFile;
ofstream outFile;

inFile.open("A:/input1.txt");
outFile.open("A:/output1.txt");

int i = 0;
int j = 0;
int temp = 0;
int sortValue[MAXVALUES/2];

for(i=0; i<MAXVALUES; i++)
{
inFile>> sortValue[i];

}

inFile.close();
for(i=0;i<MAXVALUES-1;++i)
{
for(j=0;j<MAXVALUES-1;j++)
{
if(sortValue[j+1] > sortValue[j])
{
temp = sortValue[j];
sortValue[j] = sortValue[j+1];
sortValue[j+1] = temp;
}
}
}

for(i=0;i<MAXVALUES;i++)
outFile<< "Array[" << i << "] : " << sortValue[i] << endl;

return 0;
}```

4. ## No that crashes

No that crashes

5. Supose you have a group of N numbers and would like to determine the kth largest.
I just meant that after you sorted all the elements, you could output sortValue[N/2], but im not sure what's meant by:
Draw a table showing the running time of your program for various values of N.

6. this is an Assigment, thats propably written work showing what happens within the code.
first
int sortValue[MAXVALUES/2];
your setting an array, arrays have to know how big they will be at compile time(unlesss you wana learn how to do dynmic memory allocation>
k = 4.5th item, choose how you wana round it :P
yours sorting your elements. so the 4th largest number in a 9 number array would be you 6th number <assuming your sort least->greatest>
#include <iostream>
rather then
#include< iostream >
anyway your program outputs correctly sorted things to file
you have an array all you need now is know which pay of the array you should output

For the write a table to show running times for n, i'm assuming that means show how the program is effect as the number of well numbers it must process grows larger.

7. when you say "rather then" you mean "rather than", you may say I'm anal, but this is one of the things that annoys the hell out of me. If it was a typo then apologies but FAR too many people on FAR too many forums do it, futuremark is one of the worst.

Incase you're wondering the other thing that annoys me is "m8" and other such "8" abbreviations.

EDIT: This is the part where I usually get ripped apart over my own spelling and grammar. I know it's not perfect, I never said it was.

8. Instead of using ifstream and ofstream, use a fstream and open with the ios::in and ios:ut properties.