Sorting elements in array without repetition or vectors

Hello everybody. This is my first post here on the c++ forums :D I made a code for sorting the elements in an array in ascending order without repetition without using vectors. I'm not allowed to use vectors yet because it is out of my syllabus, so I made a very crude code which can remove up to (n-1) duplicate elements and sort it in ascending order. Is there any way of improving it ?
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>
using namespace std;
int main( )
{
            int a[100], n, i, j, temp,k;
            cout<<"Enter the no. of elements : ";
            cin>> n;
            cout<<"Enter the array elements"<<endl;
            for(i=0; i< n; i++)
             {
                    cout<<"\t";
                    cin>>a[i];
             }
            //Bubble sorting
            for( i=0; i< n; i++)
            {
                   for(j=i; j< n-1; j++)
                    {
                           if(a[i]> a[j+1] )
                            {
                                 temp= a[i];
                                 a[i]= a[j+1];
                                 a[j+1]= temp;
                            }
                    }
           }
          // Removal of duplicate elements
          for (int k=0;k<n;k++)
          {
             for (i=0;i<n;i++)
             {
                    if (a[i]==a[i+1] || a[i]==a[i-1])
                    {
                         for (j=i;j<n;j++)
                                a[j]=a[j+1];
                         n--;
                    }
             }
          }
          cout<<"The sorted elements are : ";
          for (i=0;i<n;i++)
          {
                  cout<<a[i]<<" ";
          }
}
Last edited on
Looks good to me :)
I realize that it works well, but it is highly inefficient. It needs n passes to ensure there are no n-plicates. Can it be made more efficient ?
I dont really think its inefficient. The only think you can improve on is removing the second part in the if statement

if (a[i]==a[i+1] || a[i]==a[i-1])

This is enough

if (a[i] == a[i + 1])

closed account (D80DSL3A)
I've devised a single pass method for eliminating duplicates in a sorted array. It works under light testing so far.
See if you can find a flaw:
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
#include<iostream>
using std::cout;

// returns the new shorter array length
int removeDups( int a[], int sz )
{
    int currVal = a[0];// value to watch for duplicates of
    int idxRead=1, idxWrite=1;// maintain separate index values to read and write positions

    while( idxRead < sz )
    {
        if( a[idxRead] != currVal )
        {
            currVal = a[idxRead];// update current value
            a[idxWrite] = currVal;// copy new value to current write position
            ++idxWrite;// advance write pointer
        }
        ++idxRead;
    }
    return idxWrite;
}

int main ()
{
    int nums[] = {1,1,2,3,3,3,4,5,6,6,7,8,8,8,9};// 15 elements
    int SZ = sizeof( nums )/ sizeof( nums[0] );
    int newSize = removeDups( nums, SZ );// new shorter length

    cout << "newSize = " << newSize << '\n';// sb 9
    for(int i=0; i<newSize; ++i)
        cout << nums[i] << ' ';

    cout << '\n';
    return 0;
}

Removal of the duplicates before sorting would improve the sorting efficiency though. Still O(n2) but with lower n.
Last edited on
@TarikNeaj
Yes, you're right :D I originally used that code to eliminate triplicated code, but now that I've introduced the k loop, I could remove it.
@fun2code
Yes, it works for all scenarios. I've tested it. Thanks :D
Thank you all for your helpful replies.
Last edited on
@fun2code
BTW, Why do you do this
 
int SZ = sizeof( nums )/ sizeof( nums[0] );

instead of just
 
int SZ= sizeof( nums );

 
int SZ= sizeof( nums );

That returns the number of bytes in the array. We want to know how many elements (ints) are in the array, hence the division by sizeof(nums[0]).

We could also have divided by sizeof(int) to produce the same result, but that would be problematic if we later changed the type of the array (perhaps to long).
Topic archived. No new replies allowed.