problem with bubble sort

Write your question here.

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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
  #include<iostream>
#include<conio.h>
using namespace std;
int lsearch(int[], int* , int*);
int b_search(int[], int*, int*);
void deletion(int[], int*, int*);
void insertion(int[], int*, int*, int*);
void bubble_sort(int[], int*);
void display(int[], int*);
int a[20], i, n, j, temp, k, l, item;
int main()
{
    char ch;
    cout<<"How many Array Elements??"<<endl;
    cin>>n;
    cout<<"Enter the array elements"<<endl;
    for(i=0;i<n;i++)
    {
        cin>>a[i];
    }
    cout<<"Do you want to perform any operation?"<<endl;
    cin>>ch;
    while(ch=='y'||ch=='Y')
    {
        cout<<"Which operation do you want to perform??"<<endl
        <<"Press 1 for linear search"<<endl<<"Press 2 for binary search"<<endl<<
        "Press 3 for deletion"<<endl<<"Press 4 for insertion"<<endl
        <<"Press 5 for sorting the elements"<<endl<<"Press 6 for displaying array"
        <<endl<<endl;
        cin>>l;
        switch(l)
        {
            case 1:
            cout<<"Enter the element you want to search"<<endl;
            cin>>item;
            lsearch(&a[0], &n, &item);
            break;

            case 2:
            cout<<"Enter the element to be searched"<<endl;
            cin>>item;
            b_search(&a[0], &n, &item);
            break;

            case 3:
            cout<<"Enter the element you want to delete"<<endl;
            cin>>item;
            deletion(&a[0], &n, &item);
            break;

            case 4:
            cout<<"Enter the new element you want to insert"<<endl;
            cin>>item;
            cout<<"Enter the element index"<<endl;
            cin>>k;
            insertion(&a[0], &n, &item, &k);
            break;

            case 5:
            bubble_sort(&a[0], &n);
            break;

            case 6:
            display(&a[0], &n);
            break;

            default:
            cout<<"Sorry!!! No option matched"<<endl;
        }
        cout<<"Do you wish to continue??"<<endl;
        cin>>ch;
    }

    return 0;
}

int lsearch(int a[], int *n_a, int *item_a)
{
    int i;
    for(i=0;i<n;i++)
    {
        if(a[i]==item)
        {
            cout<<"Item found at "<<i+1<<" position"<<endl;
            break;
        }
    }
    if(i==n)
    {
        cout<<"Item not found"<<endl;
    }
}
int b_search(int a[], int *n_a, int *item_a)
{
    int beg, endd, mid;
    beg=0;
    endd=n-1;
    while(beg<=endd)
    {
        mid=(beg+endd)/2;
        if(item==a[mid])
        {
            cout<<"Item found at the "<<mid+1<<" position"<<endl;
            break;
        }
        else if(item>a[mid])
        {
            beg=mid+1;
        }
        else
        {
            endd=mid-1;
        }
    }
    if(beg>endd)
    {
        cout<<"Item not found"<<endl;
    }

}

void deletion(int a[],int *n_a, int *item_a)
{
    int i, k;
     for(i=0; i<n; i++)
    {
        if(a[i]==item)
        {
            k=i;
            break;
        }
    }
    i=1;
    for(i=k;i<n; i++)
    {
        a[i]=a[i+1];
    }
    n=n-1;

    cout<<"The array after element deletion is "<<endl;
    for(i=0;i<n;i++)
    {
        cout<<a[i]<<endl;
    }
}

void insertion(int a[], int *n_a, int *item_a, int *k_a)
{
    int i;
    i=n-1;
    while(i>=k-1)
    {
        a[i+1]=a[i];
        i--;
    }
    a[k-1]=item;
    n=n+1;
    cout<<endl<<endl<<"Now after inserting, the array is:"<<endl;
    for(i=0; i<n; i++)
    {
        cout<<a[i]<<endl;
    }
}

void bubble_sort(int a[], int *n_a)
{
    int i, j, temp;
    cout<<"Doing Bubble Sort:"<<endl;
    for(i=0;i<n;i++)
    {
        for(j=0;j<n;j++)
        {
            if(a[j]>a[j+1])
            {
                temp=a[j];
                a[j]=a[j+1];
                a[j+1]=temp;
            }
        }
    }
    for(i=0;i<n;i++)
    {
        cout<<a[i]<<endl;
    }
}

void display(int a[], int *n_a)
{
    int i;
    cout<<"Displaying the array"<<endl;
    for(i=0;i<n;i++)
    {
        cout<<a[i]<<"\t";
    }
}
whats the problem?
Wikipedia has pseudo-code for bubble sort. It is somehow different.
wen i run bubble sort, it gets implemented on the previously defined array and no operations of insertion or deletion r visible.

for eg, if i have an array 2, 5, 4
and i insert 7 at the first position.
7 will get inserted but wen i run bubble sort, the result wil b 2, 4, 5
instead it shud b 2, 4 ,5 , 7


Thats kinda Keskiverto's point, he's giving you a gentle hint that your bubble sort is wrong :)

Pay extra attention to line 171 :)


Line 77,93: These functions don't return anything. They should be void

This program is a really, really good example of why it is not a good idea to use globals. You modify n all over the place which makes debugging a nightmare. You pass *n_a to most functions, but never use it.

Line 156: If k is 0, you're going to reference an out of bounds element.

Lines 173,176,177: You're going to reference an out of bounds element when j=n-1 i.e. a[n] is not valid.

Topic archived. No new replies allowed.