I have to bubblesort an array, and compare the bubblesorted array with the non-bubblesort array.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45
|
#include <iostream>
#include <cstring>
#include <algorithm>
using namespace std;
void Bubblesort (int size, int array [])
{ int swaps=0, passes = 0;
bool swapped = true;
do {swapped = false;
passes++;
for (int i = 0; i < size-1; i++)
{if (array[i] > array[i+1])
{swap(array[i], array[i+1]);
swaps++;
swapped = true;
}
}
size--;} while (swapped == true);
}
int GetArray (int array[], size_t array_size)
{
int a = 0;
while(a < 1 || a > array_size)
{
//number of elements in array
cin >> a;
}
int i;
for(i = 0; i < a; i++)
{
cin >> array[i];
}
return i;
}
int main()
{
const size_t MAX_ARRAY = 1000;
int array[MAX_ARRAY];
size_t number_of_elements = GetArray(array, MAX_ARRAY);
int otherarray[MAX_ARRAY];
memcpy(array, otherarray, sizeof(array));
Bubblesort(number_of_elements, array);
cout<<array<<" "<<otherarray;
}
|
Output: 0x7ffefe55f180 0x7ffefe560120
What???
[code before also included the following and only outputted multiple "1"'s]:
[code]
void CheckArray (int otherarray[], int array[], size_t array_size)
{ //testing our NEW array through, and see each value's corresponding location in the OLD array
for (int b=0; b<array_size; b++) //check every number in the "new array"
{ int a =0;
for (int i=0; i<array_size; i++)
{if (array[b]==otherarray[a])
cout<<a+1<<" ";
else
a++;
}
}
}
[main...]
CheckArray(otherarray, array, number_of_elements);
[/code]