Move all negative elements at the end of array

Given an array of integer values which are contain negative and positive values. And our goal is to move all negative elements at the end of array. Order of negative elements are not important.

Here given code implementation process, Which takes O(n) time.

// Java Program
// Move all negative elements at the end of array
public class MyArray
{
    public void display(int[] arr, int n)
    {
        for (int i = 0; i < n; i++)
        {
            System.out.print("  " + arr[i]);
        }
        System.out.println();
    }
    // Swap the given array elements
    public void swap(int[] arr, int start, int end)
    {
        int temp = arr[start];
        arr[start] = arr[end];
        arr[end] = temp;
    }
    // Method which is move negative elements
    public void moveNegative(int[] arr, int n)
    {
        // first index
        int i = 0;
        // last index
        int j = n - 1;
        while (i < j)
        {
            if (arr[i] < 0 && arr[j] >= 0)
            {
                // When [i] index are have negative value 
                // And [j] is positive then swapping elements values
                swap(arr, i, j);
                // Modified index
                i++;
                j--;
            }
            else if (arr[i] >= 0)
            {
                i++;
            }
            else
            {
                j--;
            }
        }
    }
    public static void main(String[] args)
    {
        MyArray task = new MyArray();
        // Array which are containing positive and negative values
        int[] arr = {
            1 , -1 , 3 , 2 , -7 , -5 , 11 , 6
        };  
        int n = arr.length;
      
        System.out.println("Before Arrange : ");
        // Before move element
        task.display(arr,n);
        // Move negative elements
        task.moveNegative(arr,n);
        // After arrange
        System.out.println("After Arrange : ");
        // After move element
        task.display(arr,n);
    }
}
Move all negative elements at the end of array
Before Arrange :
  1  -1  3  2  -7  -5  11  6
After Arrange :
  1  6  3  2  11  -5  -7  -1
// C Program
// Move all negative elements at the end of array
#include<stdio.h>

void swap(int arr[], int start, int end)
{
    int temp = arr[start];
    arr[start] = arr[end];
    arr[end] = temp;
}
void split(int arr[], int size)
{
    int i = 0, j = size - 1;
    while (i < j)
    {
        if (arr[i] < 0 && arr[j] >= 0)
        {
            swap(arr, i, j);
            i++;
            j--;
        }
        else if (arr[i] >= 0)
        {
            i++;
        }
        else
        {
            j--;
        }
    }
}
// Display array elements
void display(int arr[], int size)
{
    for (int i = 0; i < size; ++i)
    {
        printf("%3d", arr[i]);
    }
    printf("\n");
}
int main()
{
    int arr[] = {
        1 , -1 , 3 , 2 , -7 , -5 , 11 , 6
    };
    int size = sizeof(arr) / sizeof(arr[0]);
    display(arr, size);
    split(arr, size);
    //after
    display(arr, size);
}

Output

  1 -1  3  2 -7 -5 11  6
  1  6  3  2 11 -5 -7 -1
// Include header file
#include <iostream>
using namespace std;
// C++ Program
// Move all negative elements at the end of array
class MyArray
{
    public: void display(int arr[], int n)
    {
        for (int i = 0; i < n; i++)
        {
            cout << "  " << arr[i];
        }
        cout << endl;
    }
    // Swap the given array elements
    void swap(int arr[], int start, int end)
    {
        int temp = arr[start];
        arr[start] = arr[end];
        arr[end] = temp;
    }
    // Method which is move negative elements
    void moveNegative(int arr[], int n)
    {
        // first index
        int i = 0;
        // last index
        int j = n - 1;
        while (i < j)
        {
            if (arr[i] < 0 && arr[j] >= 0)
            {
                // When [i] index are have negative value 
                // And [j] is positive then swapping elements values
                this->swap(arr, i, j);
                // Modified index
                i++;
                j--;
            }
            else if (arr[i] >= 0)
            {
                i++;
            }
            else
            {
                j--;
            }
        }
    }
};
int main()
{
    MyArray *task = new MyArray();
    // Array which are containing positive and negative values
    int arr[] = {
        1 , -1 , 3 , 2 , -7 , -5 , 11 , 6
    };
    int n = sizeof(arr) / sizeof(arr[0]);
    cout << "Before Arrange : " << endl;
    // Before move element
    task->display(arr, n);
    // Move negative elements
    task->moveNegative(arr, n);
    // After arrange
    cout << "After Arrange : " << endl;
    // After move element
    task->display(arr, n);
    return 0;
}

Output

Before Arrange :
  1  -1  3  2  -7  -5  11  6
After Arrange :
  1  6  3  2  11  -5  -7  -1
package main
import "fmt"
// Go Program
// Move all negative elements at the end of array
type MyArray struct {}
func getMyArray() * MyArray {
    var me *MyArray = &MyArray {}
    return me
}
func(this MyArray) display(arr[] int, n int) {
    for i := 0 ; i < n ; i++ {
        fmt.Print("  ", arr[i])
    }
    fmt.Println()
}
// Swap the given array elements
func(this MyArray) swap(arr[] int, start int, end int) {
    var temp int = arr[start]
    arr[start] = arr[end]
    arr[end] = temp
}
// Method which is move negative elements
func(this MyArray) moveNegative(arr[] int, n int) {
    // first index
    var i int = 0
    // last index
    var j int = n - 1
    for (i < j) {
        if arr[i] < 0 && arr[j] >= 0 {
            // When [i] index are have negative value 
            // And [j] is positive then swapping elements values
            this.swap(arr, i, j)
            // Modified index
            i++
            j--
        } else if arr[i] >= 0 {
            i++
        } else {
            j--
        }
    }
}
func main() {
    var task * MyArray = getMyArray()
    // Array which are containing positive and negative values
    var arr = [] int {1 , -1 , 3 , 2 , -7 , -5 , 11 , 6}
    var n int = len(arr)
    fmt.Println("Before Arrange : ")
    // Before move element
    task.display(arr, n)
    // Move negative elements
    task.moveNegative(arr, n)
    // After arrange
    fmt.Println("After Arrange : ")
    // After move element
    task.display(arr, n)
}

Output

Before Arrange :
  1  -1  3  2  -7  -5  11  6
After Arrange :
  1  6  3  2  11  -5  -7  -1
// Include namespace system
using System;
// Csharp Program
// Move all negative elements at the end of array
public class MyArray
{
    public void display(int[] arr, int n)
    {
        for (int i = 0; i < n; i++)
        {
            Console.Write("  " + arr[i]);
        }
        Console.WriteLine();
    }
    // Swap the given array elements
    public void swap(int[] arr, int start, int end)
    {
        int temp = arr[start];
        arr[start] = arr[end];
        arr[end] = temp;
    }
    // Method which is move negative elements
    public void moveNegative(int[] arr, int n)
    {
        // first index
        int i = 0;
        // last index
        int j = n - 1;
        while (i < j)
        {
            if (arr[i] < 0 && arr[j] >= 0)
            {
                // When [i] index are have negative value 
                // And [j] is positive then swapping elements values
                this.swap(arr, i, j);
                // Modified index
                i++;
                j--;
            }
            else if (arr[i] >= 0)
            {
                i++;
            }
            else
            {
                j--;
            }
        }
    }
    public static void Main(String[] args)
    {
        MyArray task = new MyArray();
        // Array which are containing positive and negative values
        int[] arr = {
            1 , -1 , 3 , 2 , -7 , -5 , 11 , 6
        };
        int n = arr.Length;
        Console.WriteLine("Before Arrange : ");
        // Before move element
        task.display(arr, n);
        // Move negative elements
        task.moveNegative(arr, n);
        // After arrange
        Console.WriteLine("After Arrange : ");
        // After move element
        task.display(arr, n);
    }
}

Output

Before Arrange :
  1  -1  3  2  -7  -5  11  6
After Arrange :
  1  6  3  2  11  -5  -7  -1
<?php
// Php Program
// Move all negative elements at the end of array
class MyArray
{
    public  function display($arr, $n)
    {
        for ($i = 0; $i < $n; $i++)
        {
            echo("  ".$arr[$i]);
        }
        echo("\n");
    }
    // Swap the given array elements
    public  function swap(&$arr, $start, $end)
    {
        $temp = $arr[$start];
        $arr[$start] = $arr[$end];
        $arr[$end] = $temp;
    }
    // Method which is move negative elements
    public  function moveNegative(&$arr, $n)
    {
        // first index
        $i = 0;
        // last index
        $j = $n - 1;
        while ($i < $j)
        {
            if ($arr[$i] < 0 && $arr[$j] >= 0)
            {
                // When [i] index are have negative value 
                // And [j] is positive then swapping elements values
                $this->swap($arr, $i, $j);
                // Modified index
                $i++;
                $j--;
            }
            else if ($arr[$i] >= 0)
            {
                $i++;
            }
            else
            {
                $j--;
            }
        }
    }
}

function main()
{
    $task = new MyArray();
    // Array which are containing positive and negative values
    $arr = array(1, -1, 3, 2, -7, -5, 11, 6);
    $n = count($arr);
    echo("Before Arrange : \n");
    // Before move element
    $task->display($arr, $n);
    // Move negative elements
    $task->moveNegative($arr, $n);
    // After arrange
    echo("After Arrange : \n");
    // After move element
    $task->display($arr, $n);
}
main();

Output

Before Arrange :
  1  -1  3  2  -7  -5  11  6
After Arrange :
  1  6  3  2  11  -5  -7  -1
// Node JS Program
// Move all negative elements at the end of array
class MyArray
{
    display(arr, n)
    {
        for (var i = 0; i < n; i++)
        {
            process.stdout.write("  " + arr[i]);
        }
        process.stdout.write("\n");
    }
    // Swap the given array elements
    swap(arr, start, end)
    {
        var temp = arr[start];
        arr[start] = arr[end];
        arr[end] = temp;
    }
    // Method which is move negative elements
    moveNegative(arr, n)
    {
        // first index
        var i = 0;
        // last index
        var j = n - 1;
        while (i < j)
        {
            if (arr[i] < 0 && arr[j] >= 0)
            {
                // When [i] index are have negative value 
                // And [j] is positive then swapping elements values
                this.swap(arr, i, j);
                // Modified index
                i++;
                j--;
            }
            else if (arr[i] >= 0)
            {
                i++;
            }
            else
            {
                j--;
            }
        }
    }
}

function main()
{
    var task = new MyArray();
    // Array which are containing positive and negative values
    var arr = [1, -1, 3, 2, -7, -5, 11, 6];
    var n = arr.length;
    console.log("Before Arrange : ");
    // Before move element
    task.display(arr, n);
    // Move negative elements
    task.moveNegative(arr, n);
    // After arrange
    console.log("After Arrange : ");
    // After move element
    task.display(arr, n);
}
main();

Output

Before Arrange :
  1  -1  3  2  -7  -5  11  6
After Arrange :
  1  6  3  2  11  -5  -7  -1
#  Python 3 Program
#  Move all negative elements at the end of array
class MyArray :
    def display(self, arr, n) :
        i = 0
        while (i < n) :
            print("  ", arr[i], end = "")
            i += 1
        
        print(end = "\n")
    
    #  Swap the given list elements
    def swap(self, arr, start, end) :
        temp = arr[start]
        arr[start] = arr[end]
        arr[end] = temp
    
    #  Method which is move negative elements
    def moveNegative(self, arr, n) :
        #  first index
        i = 0
        #  last index
        j = n - 1
        while (i < j) :
            if (arr[i] < 0 and arr[j] >= 0) :
                #  When [i] index are have negative value 
                #  And [j] is positive then swapping elements values
                self.swap(arr, i, j)
                #  Modified index
                i += 1
                j -= 1
            elif (arr[i] >= 0) :
                i += 1
            else :
                j -= 1
            
        
    

def main() :
    task = MyArray()
    #  Array which are containing positive and negative values
    arr = [1, -1, 3, 2, -7, -5, 11, 6]
    n = len(arr)
    print("Before Arrange : ")
    #  Before move element
    task.display(arr, n)
    #  Move negative elements
    task.moveNegative(arr, n)
    #  After arrange
    print("After Arrange : ")
    #  After move element
    task.display(arr, n)

if __name__ == "__main__": main()

Output

Before Arrange :
   1   -1   3   2   -7   -5   11   6
After Arrange :
   1   6   3   2   11   -5   -7   -1
#  Ruby Program
#  Move all negative elements at the end of array
class MyArray 
    def display(arr, n) 
        i = 0
        while (i < n) 
            print("  ", arr[i])
            i += 1
        end

        print("\n")
    end

    #  Swap the given array elements
    def swap(arr, start, last) 
        temp = arr[start]
        arr[start] = arr[last]
        arr[last] = temp
    end

    #  Method which is move negative elements
    def moveNegative(arr, n) 
        #  first index
        i = 0
        #  last index
        j = n - 1
        while (i < j) 
            if (arr[i] < 0 && arr[j] >= 0) 
                #  When [i] index are have negative value 
                #  And [j] is positive then swapping elements values
                self.swap(arr, i, j)
                #  Modified index
                i += 1
                j -= 1
            elsif (arr[i] >= 0) 
                i += 1
            else
 
                j -= 1
            end

        end

    end

end

def main() 
    task = MyArray.new()
    #  Array which are containing positive and negative values
    arr = [1, -1, 3, 2, -7, -5, 11, 6]
    n = arr.length
    print("Before Arrange : ", "\n")
    #  Before move element
    task.display(arr, n)
    #  Move negative elements
    task.moveNegative(arr, n)
    #  After arrange
    print("After Arrange : ", "\n")
    #  After move element
    task.display(arr, n)
end

main()

Output

Before Arrange : 
  1  -1  3  2  -7  -5  11  6
After Arrange : 
  1  6  3  2  11  -5  -7  -1
// Scala Program
// Move all negative elements at the end of array
class MyArray()
{
    def display(arr: Array[Int], n: Int): Unit = {
        var i: Int = 0;
        while (i < n)
        {
            print("  " + arr(i));
            i += 1;
        }
        print("\n");
    }
    // Swap the given array elements
    def swap(arr: Array[Int], start: Int, end: Int): Unit = {
        var temp: Int = arr(start);
        arr(start) = arr(end);
        arr(end) = temp;
    }
    // Method which is move negative elements
    def moveNegative(arr: Array[Int], n: Int): Unit = {
        // first index
        var i: Int = 0;
        // last index
        var j: Int = n - 1;
        while (i < j)
        {
            if (arr(i) < 0 && arr(j) >= 0)
            {
                // When [i] index are have negative value 
                // And [j] is positive then swapping elements values
                swap(arr, i, j);
                // Modified index
                i += 1;
                j -= 1;
            }
            else if (arr(i) >= 0)
            {
                i += 1;
            }
            else
            {
                j -= 1;
            }
        }
    }
}
object Main
{
    def main(args: Array[String]): Unit = {
        var task: MyArray = new MyArray();
        // Array which are containing positive and negative values
        var arr: Array[Int] = Array(1, -1, 3, 2, -7, -5, 11, 6);
        var n: Int = arr.length;
        println("Before Arrange : ");
        // Before move element
        task.display(arr, n);
        // Move negative elements
        task.moveNegative(arr, n);
        // After arrange
        println("After Arrange : ");
        // After move element
        task.display(arr, n);
    }
}

Output

Before Arrange :
  1  -1  3  2  -7  -5  11  6
After Arrange :
  1  6  3  2  11  -5  -7  -1
import Foundation;
// Swift 4 Program
// Move all negative elements at the end of array
class MyArray
{
    func display(_ arr: [Int], _ n: Int)
    {
        var i: Int = 0;
        while (i < n)
        {
            print("  ", arr[i], terminator: "");
            i += 1;
        }
        print(terminator: "\n");
    }
    // Swap the given array elements
    func swap(_ arr: inout[Int], _ start: Int, _ end: Int)
    {
        let temp: Int = arr[start];
        arr[start] = arr[end];
        arr[end] = temp;
    }
    // Method which is move negative elements
    func moveNegative(_ arr: inout[Int], _ n: Int)
    {
        // first index
        var i: Int = 0;
        // last index
        var j: Int = n - 1;
        while (i < j)
        {
            if (arr[i] < 0 && arr[j] >= 0)
            {
                // When [i] index are have negative value 
                // And [j] is positive then swapping elements values
                self.swap(&arr, i, j);
                // Modified index
                i += 1;
                j -= 1;
            }
            else if (arr[i] >= 0)
            {
                i += 1;
            }
            else
            {
                j -= 1;
            }
        }
    }
}
func main()
{
    let task: MyArray = MyArray();
    // Array which are containing positive and negative values
    var arr: [Int] = [1, -1, 3, 2, -7, -5, 11, 6];
    let n: Int = arr.count;
    print("Before Arrange : ");
    // Before move element
    task.display(arr, n);
    // Move negative elements
    task.moveNegative(&arr, n);
    // After arrange
    print("After Arrange : ");
    // After move element
    task.display(arr, n);
}
main();

Output

Before Arrange :
   1   -1   3   2   -7   -5   11   6
After Arrange :
   1   6   3   2   11   -5   -7   -1
// Kotlin Program
// Move all negative elements at the end of array
class MyArray
{
    fun display(arr: Array < Int > , n: Int): Unit
    {
        var i: Int = 0;
        while (i < n)
        {
            print("  " + arr[i]);
            i += 1;
        }
        print("\n");
    }
    // Swap the given array elements
    fun swap(arr: Array < Int > , start: Int, end: Int): Unit
    {
        val temp: Int = arr[start];
        arr[start] = arr[end];
        arr[end] = temp;
    }
    // Method which is move negative elements
    fun moveNegative(arr: Array < Int > , n: Int): Unit
    {
        // first index
        var i: Int = 0;
        // last index
        var j: Int = n - 1;
        while (i < j)
        {
            if (arr[i] < 0 && arr[j] >= 0)
            {
                // When [i] index are have negative value 
                // And [j] is positive then swapping elements values
                this.swap(arr, i, j);
                // Modified index
                i += 1;
                j -= 1;
            }
            else if (arr[i] >= 0)
            {
                i += 1;
            }
            else
            {
                j -= 1;
            }
        }
    }
}
fun main(args: Array < String > ): Unit
{
    val task: MyArray = MyArray();
    // Array which are containing positive and negative values
    var arr: Array < Int > = arrayOf(1, -1, 3, 2, -7, -5, 11, 6);
    val n: Int = arr.count();
    println("Before Arrange : ");
    // Before move element
    task.display(arr, n);
    // Move negative elements
    task.moveNegative(arr, n);
    // After arrange
    println("After Arrange : ");
    // After move element
    task.display(arr, n);
}

Output

Before Arrange :
  1  -1  3  2  -7  -5  11  6
After Arrange :
  1  6  3  2  11  -5  -7  -1


Please share your knowledge to improve code and content standard. Also submit your doubts, and test case. We improve by your feedback. We will try to resolve your query as soon as possible.

New Comment







© 2021, kalkicode.com, All rights reserved