bubble sort for a code

Jan 17, 2015 at 6:34pm
Write your question here.
i need to add a bubble sort to this code and i coudn't do it,please a save from someone i need it in two and half hours from now

Thank you

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
46
47
48
49
50
51
52
53
54
55
# include <iostream>
#define T=5
using namespace std;
int arrays(int number1[],int number2[])
{
int x=5,i,a,b=0;
for (i=0;i<5;i++)
{
    for (a=0;a<x;a++)
	           {
            if (number1[i]==number2[a])
{
    number2[a]=number2[x];
         number1[i]=number1[x];
          x--;
          a--;
          b++;
        continue;
}
	           }
    if (number1[i]==number2[a])
          {number1[i]=number1[x];
          x--;
          a--;
          b++;
          continue;
          }
}
return x;}

int main ()
{
 int x,i,a,num1[5],num2[5],b=0;
 cout<< "Please enter 5 intigers: "<<endl;
                for (i=0;i<5;i++)
{
    cin>> num1[i];}
    cout<< "Please enter another 5 intigers: "<<endl;
    for (a=0;a<5;a++)
    {
        cin>> num2[a];
    }

x=arrays(num1,num2);
    if (x==0)
    {
        cout <<"This arrays have same numbers "<<endl;
    }
    else
    {
        cout <<"This arrays dose not have same numbers "<<endl;
    }
 return 0;
 }
Last edited on Jan 17, 2015 at 6:35pm
Jan 17, 2015 at 6:51pm
http://rosettacode.org/wiki/Sorting_algorithms/Bubble_sort
We can help you write your own, but we will not do it for you
Jan 17, 2015 at 7:13pm
i know usually i am studying what i am being told but they didn't taught us that and this is new and i two hours to figure this out please mate help me with that

i have tried to use the simple command if(swap())
without any success
Last edited on Jan 17, 2015 at 7:14pm
Jan 17, 2015 at 7:20pm
Okay so swap is where you started to have trouble?
Here is how to swap two integers:
1
2
3
4
5
6
7
int a=5, b=2;

int temp; //we need a temporary location to store a copy
temp = a;  //store the first number to the temporary location
a = b;  //overwrite the first number with the second
//it is okay to overwrite because the temporary location has a copy
b = temp;  //the two numbers are now officially swapped 


The goal of bubble sort is to search the part of the list that is not sorted yet for the biggest or smallest element. Take that element and put it on the end of the sorted part of the list by "bubbling" the element to the correct spot.
Last edited on Jan 17, 2015 at 7:21pm
Jan 17, 2015 at 7:39pm
i have tried now to put it in the code without any success, sorry bro i am too tired and this is new thing for mem i set on Hw all day long i am going to upload it like that,thank you for trying to help me
Last edited on Jan 17, 2015 at 7:40pm
Topic archived. No new replies allowed.