Home > other >  Rearrange array non-negative numbers to the left and into ascending order C
Rearrange array non-negative numbers to the left and into ascending order C

Time:07-22

I already arrange non-negative numbers to the left side of the array, now I want to put the sort function to rearrange numbers in ascending order into my program but it didn't work, I can't have them both, can you all help please? I'm totally new to this.

int tg;
    for(int i = 0; i < n - 1; i  ){
        for(int j = i   1; j < n; j  ){
            if(a[i] > a[j]){
                tg = a[i];
                a[i] = a[j];
                a[j] = tg;        
            }
        }
    }
#include<bits/stdc  .h> 
using namespace std; 
  
void segregateElements(int arr[], int n) 
{ 
    int temp[n]; 
    int j = 0; // index of temp 
    for (int i = 0; i < n ; i  ) 
        if (arr[i] >= 0 ) 
            temp[j  ] = arr[i]; 
    
    if (j == n || j == 0) 
        return; 

    for (int i = 0 ; i < n ; i  ) 
        if (arr[i] < 0) 
            temp[j  ] = arr[i]; 
  
    memcpy(arr, temp, sizeof(temp)); 
} 
  
int main() 
{ 
    int arr[] = {1 ,-1 ,-3 , -2, 7, 5, 11, 6 }; 
    int n = sizeof(arr)/sizeof(arr[0]); 
  
    segregateElements(arr, n); 
  
    for (int i = 0; i < n; i  ) 
    cout << arr[i] << " "; 
  
    return 0; 
} 

*Output : 1 7 5 11 6 -1 -3 -2 * Expected: 1 5 6 7 11 -1 -2 -3

CodePudding user response:

You have two requirements:

  • sort array elements by ascending order
  • re-arrange non-negative numbers into the left part of the array

So you need to add left rotation after sorting is done. Check the following code:

#include<iostream>
using namespace std;

/*Function to get gcd of a and b*/
int gcd(int a, int b)
{
    if (b == 0)
        return a;

    else
        return gcd(b, a % b);
}

/*Function to left rotate arr[] of size n by d*/
void leftRotate(int arr[], int d, int n)
{
    /* To handle if d >= n */
    d = d % n;
    int g_c_d = gcd(d, n);
    for (int i = 0; i < g_c_d; i  ) {
        /* move i-th values of blocks */
        int temp = arr[i];
        int j = i;

        while (1) {
            int k = j   d;
            if (k >= n)
                k = k - n;

            if (k == i)
                break;

            arr[j] = arr[k];
            j = k;
        }
        arr[j] = temp;
    }
}
void sortElements(int a[], int n)
{
    int tg;
    for(int i = 0; i < n - 1; i  ){
        for(int j = i   1; j < n; j  ){
            if(a[i] > a[j] && a[i]){
                tg = a[i];
                a[i] = a[j];
                a[j] = tg;
            }
        }
    }
    int pivot = -1;

    for(int i=0; i < n; i  ){
        if(a[i] >= 0){
            pivot = i;
            break;
        }
    }
    leftRotate(a, pivot, n);

}

int main()
{
    int arr[] = {1, 6, -5, 2, 3, 11, -7, -1};
    int n = sizeof(arr)/sizeof(arr[0]);

    sortElements(arr, n);

    for (int i = 0; i < n; i  )
    cout << arr[i] << " ";

    return 0;
}

CodePudding user response:

First,a part of your code is this

int arr[] = {1 ,-1 ,-3 , -2, 7, 5, 11, 6 };

but,the output you want is

1 2 3 6 11 -1 -5 -7

You maybe make some mistakes : there is no -7 in the arr

I think the code below will solve your problem

#include <cstring>
#include <iostream>
using namespace std; 
  
void segregateElements(int arr[], int n) 
{ 
    int temp[n]; 
    int j = 0; // index of temp 
    for (int i = 0; i < n ; i  ) 
        if (arr[i] >= 0 ) 
            temp[j  ] = arr[i]; 
    if (j == 0 || j == 1) 
        return; 
    for(int i = 0;i < j;i  ){
        for (int k = 0;k < j - 1;k  ){
            if (temp[k 1]<temp[k]){
                int tg = temp[k 1];
                temp[k 1] = temp[k];
                temp[k] = tg;
            }
        }
    }
    for (int i = j;i < n;i  ){
        for (int k = j;k < n - 1;k  ){
            if (temp[k 1]<temp[k]){
                int tg = temp[k 1];
                temp[k 1] = temp[k];
                temp[k] = tg;
            }
        }
    }  

    for (int i = 0 ; i < n ; i  ) 
        if (arr[i] < 0) 
            temp[j  ] = arr[i]; 
  
    memcpy(arr, temp, sizeof(temp)); 
} 
  
int main() 
{ 
    int arr[] = {1 ,-1 ,-3 , -2, 7, 5, 11, 6 }; 
    int n = sizeof(arr)/sizeof(arr[0]); 
  
    segregateElements(arr, n); 
  
    for (int i = 0; i < n; i  ) 
        cout << arr[i] << " "; 
    cout <<endl;
    return 0; 
} 
  • Related