简体   繁体   中英

Rearranging an array of integers

I need to implement the following in both pseudocode and java.

Input: an array of integers

Output: Rearrange the array to have the following:

  1. suppose the first element in the original array has the value x
  2. In the new array, suppose that x is in position I, that is data[I]=x. Then, data[j] <= x for all jx for all j>I. This means that all the values to the "left" of x are less than or equal to x and all the values to the "right" are larger than x.
  3. An example is as follows: Suppose the array has the elements in this initial order: 4,3,9,2,7,6,5. After applying your algorithm, you should get: 3,2,4,5,9,7,6. That is, the leftmost element, 4, is positioned in the resulting array so that all elements less than 4 (2 and 3) are to its left (in no particular order), and all elements larger than 4 are to its right (in no particular order).

There is no space requirement for the algorithm, only that the problem is solved in O(n) time.

Thus, I am under the impression that a bubble sort is best here.

A swapping algorithm in this case is not the best option and I would like to get some feedback on other methods that can be implemented here.

Thank you!

create an array with space to fit all the elements. If number is < x then place it at the beginning of the array, if number is > x then place it at the end. If number is equal to x then just ignore it and move on. Finally you fill up the remaining spots with the values equal to x.

java.util.array does what you are saying, but then i maybe missing a critical implemetation detail: this is it:

int[] numbers = {4, 9, 1, 3, 2, 8, 7, 0, 6, 5};



System.out.println(Arrays.toString(numbers));

java.util.Arrays.sort(numbers);
System.out.println(Arrays.toString(numbers));

Output:

Before sorting: [4, 9, 1, 3, 2, 8, 7, 0, 6, 5]
After sorting: [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]

using integer 4 as the reference point, all the conditions you listed are met. the native sorting algorithm will be way faster. but if you need to be in control try the first answer

to get all the value left of 4, just locate the index of 4 lets say i[nth] and return all index lower than i[nth]. to get the numbers right of 4, locate index of 4 ie i[nth] and get the index higher than i[nth] while I[nth] less than the length of the array for [zero-based array]

Given the OP's small data set, a bubble sort would work, but "...bubble sort is not a practical sorting algorithm when n is large." (see bubblesort ). An efficient algorithm is quicksort :

...the algorithm takes O(n log n) comparisons to sort n items.

The following implementation of QuickSort is a fork I obtained from Ideone , and modified to include the OP array of numbers and the code pivots on the array's left value, as follows:

import java.util.*;
import java.lang.*;
import java.io.*;

class Ideone
{


    public static void main (String[] args) throws java.lang.Exception
    {
        int b[] = { 4, 3, 9, 2, 7, 6, 5 };
        sort( b, 0, b.length - 1 );
        System.out.println( Arrays.toString( b ) );
    }

    static void sort(int a[], int left, int right)   {  
       if (left < right){
        int i=left, j=right, tmp;    
        int v = a[left]; //pivot

        do {
            while( a[i] < v)
              i++;
            while(a[j]>v) 
              j--;

            if( i <= j){            
               tmp = a[i];
               a[i] = a[j];
               a[j] = tmp;
               i++;            
               j--;
             }
       }  while( i <=  j );        

        if( left < j )  sort( a, left, j );
        if( i < right ) sort(a,i,right);
      }
   }
}

Live code

Note: this solution uses recursion by having sort() call itself depending on the values of left and i in contrast respectively to j and right .

Alternatively, you could do as @Maxwell suggests and use Java's native sort() since it is a speedy implementation of QuickSort (see here ).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM