C++ shift values to begin of array

stoner

Need to move all values which is less than 1 in begin of array (WITHOUT SORT, and need solution without second array)

for example:

START ARRAY: {-2.12, -3, 7.36, 6.83, -1.82, 7.01}

FINISH ARRAY: {-2.12, -3, -1.82, 7.36, 6.83, 7.01}

There is my solution but it doesn't work very well, because at final we receive:

FINISH ARRAY: {-2.12, -3, -1.82, 6.83, 7.36, 7.01}

Values which less than 1, moves to begin of array, but 4 and 5 elements not in correct order

#include <iostream>

using namespace std;

int main() {
    double arr[6] = {-2.12, -3, 7.36, 6.83, -1.82, 7.01};

    cout << "Start array: " << endl;
    for (int x = 0; x < 6; x++) {
        cout << arr[x] << ", ";
    }

    int index=0;
    double temp;
    for (int i = 0; i < 6; i++) { 
        if (arr[i] < 1) {
            temp=arr[i];
            arr[i] = arr[index];
            arr[index] = temp;
            index++;
        }
     }
    cout << endl << "FINISH ARRAY: " << endl;
    for (int x = 0; x < 6; x++) {
        cout << arr[x] << ", ";
    }



    return 0;
}
Evg

Use std::stable_partition:

std::stable_partition(std::begin(arr), std::end(arr), 
    [](double d) { return d < 1; });

If you want to implement it yourself, note, that in-place stable partition (using comparisons and swaps) cannot be done better than in O(N log N) time. Any algorithm with O(N) running time is incorrect.

One possible solution can be obtained with divide-and-conquer approach:

template<class It, class Pred>
It stable_partition(It first, It last, Pred pred) {
    // returns the iterator to the first element of the second group:
    // TTTTTFFFFFF
    //      ^ return value

    if (first == last)
        return last;
    if (last - first == 1) {
        if (pred(*first))     // T
            return last;      //  ^ last  
        else                  // F
            return first;     // ^ first
    }

    // Split in two halves
    const auto mid = first + (last - first) / 2;

    // Partition the left half
    const auto left = stable_partition(first, mid, pred);
    // TTTTTFFFFF
    //      ^ left
    //           ^ mid 

    // Partition the right half
    const auto right = stable_partition(mid, last, pred);
    // TTTTTFFFFF
    //      ^ right
    // ^ mid

    // Rotate the middle part: TTTTTFFFFFTTTTTFFFFF
    //                              ~~~~~~~~~~
    //                              ^ left    ^ right
    //                                   ^ mid
    const auto it =  std::rotate(left, mid, right);
    // TTTTTTTTTTFFFFFFFFFF
    //           ^ it
    return it;
}

It resembles quicksort, but here we do not actually sort the range. std::rotate itself can be easily implemented via three reverses.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Shift values of typed Javascript array?

how to shift values in an array in python

Shift through an array sequentially in C

How to shift values inside array in javascript

Shift values in numpy array by differing amounts

How to shift values in an array to the left by 3

Parsing values from Byte array C# using Shift and Bitware AND operatorts

Get the return type of begin on a c array

Create array in Ruby with begin value, end value and step for float values

Shift values from array into one array without key

looking for a better way to shift array of characters in C

How to shift an array elements by n places in C

How to locally shift values in a numpy array by arrays of shifts?

Generate a fixed-length array, but push() and shift() to keep changing values?

C++ Pointer array to set using STL begin and end

How does the C++ begin and end work when argument is an array?

constexpr begin of a std::array

Expected begin array but was begin object in Retrofit call

Expected BEGIN_ARRAY but was BEGIN_OBJECT

BEGIN_ARRAY but was BEGIN_OBJECT

Sorting values in an array in C

Appending values to c array

C++ pointer of array: why cannot use an array defined by indirectly computed const var in function begin(array)?

New array from existing one, 2 column begin indexes of line/colum from the existing, third being values

C++ how to use a pointer to circular shift array element

How to delete same numbers and shift the array to delete them in c

How to shift 2d array column up in c program

C#: Shift given rows of jagged array to the top

c++ left shift operator used inside array declaration