#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <iostream>
#define BUBBLE 1000
using namespace std;
int main()
{
int myArray[1000];
int i, j;
int temp = 0;
int num;
srand(time(NULL));
//fill array
for (i = 0; i < 1000; i ++)
{
myArray[i]=(rand()%250)+1;
cout<<myArray[i]<<" ";
}
cout<<endl<<endl;
trying to sort 1000 numbers between 0 and 250 into ascending order, codeblocks is telling me i,j are unused variables. if somebody could fix this for me id appreciate it :) thanks
it should sort an array of 1000 random numbers between 0 and 250 into ascending order, and the output should be that vector. Sorry for the late reply
[code]
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <iostream>
#define BUBBLE 1000
using namespace std;
int main()
{
int myArray[1000];
int i;
int temp = 0;
int num;
srand(time(NULL));
//fill array
for (i = 0; i < 1000; i ++)
{
myArray[i]=(rand()%250)+1;
cout<<myArray[i]<<" ";
}
cout<<endl<<endl;
1. If myArray[i] is less than myArray[i+1] then they are in the expected order. So no swapping is needed in this case.
2. Try a look at Wikipedia for Bubblesort.
3. Sorry, but as you should see yourself: Your code stays unreadable for us. Format it using the "<>"-button:
3.1 Indent your code lines
3.2 Mark all code lines
3.3 Press "<>"-button
3.4 Press "Preview"-button and have a look at the resulting text
3.5 Repeat 3.1 .. 3.4 until code is readable for everyone.
Another tipp: Get some small pieces of paper. Write any number on each one. Distribute them randomly in a row. Now try to sort them using the BubbleSort algorithm. This may really help you to understand the procedure much better!