[英]Sorting two non-sequential arrays to one array ascending
I was trying to sort two arrays with non sequential numbers into one array after I did with sequential numbers. 在我对序列号进行处理之后,我试图将两个非序列号的数组排序为一个数组。 Do I need to order the arrays separately or is there a more effective way?
我需要单独订购阵列还是有更有效的方法?
If I run the code below my output will be 4,16,2,11,19.. and it should be 0,1,2,3,4.. 如果我在下面运行代码,我的输出将是4,16,2,11,19 ..并且它应该是0,1,2,3,4 ..
int myFirstArray [] = { 16, 2, 11, 34, 77, 1, 0, 10, 3 };
int mySecondArray [] = { 4, 19, 6, 32, 8, 10, 66 };
int firstPos = 0, secondPos = 0;
int myThirdArray [] = new int[myFirstArray.length + mySecondArray.length];
for (int i = 0; i < myThirdArray.length; i++) {
if (firstPos < myFirstArray.length && secondPos < mySecondArray.length) {
if (mySecondArray[secondPos] < myFirstArray[firstPos]) {
myThirdArray[i] = mySecondArray[secondPos];
secondPos++;
}
else {
myThirdArray[i] = myFirstArray[firstPos];
firstPos++;
}
}
else if (secondPos < mySecondArray.length) {
myThirdArray[i] = mySecondArray[secondPos];
secondPos++;
}
else {
myThirdArray[i] = myFirstArray[firstPos];
firstPos++;
}
}
for(int i = 0; i < myThirdArray.length; i++) {
System.out.println(myThirdArray[i]);
}
If you had 2 sorted arrays and want to combine them into one sorted array then your code would be correct. 如果您有2个排序数组,并且想要将它们组合成一个排序数组,那么您的代码将是正确的。 But you are comparing the first 2 elements of your unsorted arrays and create a topically sorted array, meaning some elements of the array are sorted compared to others ie
4 < 16
, 2 < 11 < 19
. 但是,您正在比较未排序数组的前2个元素,并创建一个局部排序的数组,这意味着该数组的某些元素与其他元素进行了排序,例如
4 < 16
2 < 11 < 19
。
Your logic is not far away from Mergesort . 您的逻辑离Mergesort不远。 You split your array into halves and split them again and merges the 2 halves.
您将数组拆分为两半,然后再次拆分为两半。 You end up merging arrays of size 1, then merging arrays of size 2 and so on and so on.
您最终合并大小为1的数组,然后合并大小为2的数组,依此类推。 Your merging code is correct.
您的合并代码是正确的。 You can see more details here .
您可以在此处查看更多详细信息。
// Merges two subarrays of arr[].
// First subarray is arr[l..m]
// Second subarray is arr[m+1..r]
void merge(int arr[], int l, int m, int r)
{
// Find sizes of two subarrays to be merged
int n1 = m - l + 1;
int n2 = r - m;
/* Create temp arrays */
int L[] = new int [n1];
int R[] = new int [n2];
/*Copy data to temp arrays*/
for (int i=0; i<n1; ++i)
L[i] = arr[l + i];
for (int j=0; j<n2; ++j)
R[j] = arr[m + 1+ j];
/* Merge the temp arrays */
// Initial indexes of first and second subarrays
int i = 0, j = 0;
// Initial index of merged subarry array
int k = l;
while (i < n1 && j < n2)
{
if (L[i] <= R[j])
{
arr[k] = L[i];
i++;
}
else
{
arr[k] = R[j];
j++;
}
k++;
}
/* Copy remaining elements of L[] if any */
while (i < n1)
{
arr[k] = L[i];
i++;
k++;
}
/* Copy remaining elements of R[] if any */
while (j < n2)
{
arr[k] = R[j];
j++;
k++;
}
}
// Main function that sorts arr[l..r] using
// merge()
void sort(int arr[], int l, int r)
{
if (l < r)
{
// Find the middle point
int m = (l+r)/2;
// Sort first and second halves
sort(arr, l, m);
sort(arr , m+1, r);
// Merge the sorted halves
merge(arr, l, m, r);
}
}
It is always better to sort your both arrays first and then merge both the array in single array. 最好先对两个数组进行排序,然后再将两个数组合并为一个数组。
// Function to merge array in sorted order
// a[] will be the first unsorted array.
// b[] will be the second unsorted array.
public static int[] sortedMerge(int a[], int b[]){
int n = a.length;
int m = b.length;
int totalSize=n+m;
//result array
int[] res =new int[totalSize];
// Sorting a[] and b[]
Arrays.sort(a);
Arrays.sort(b);
// Merge two sorted arrays into res[]
int i = 0, j = 0, k = 0;
while (i < n && j < m) {
if (a[i] <= b[j]) {
res[k] = a[i];
i += 1;
k += 1;
} else {
res[k] = b[j];
j += 1;
k += 1;
}
}
while (i < n) { // Merging remaining
// elements of a[] (if any)
res[k] = a[i];
i += 1;
k += 1;
}
while (j < m) { // Merging remaining
// elements of b[] (if any)
res[k] = b[j];
j += 1;
k += 1;
}
return res;
}
This way the Time Complexity will be O(nlogn + mlogm + (n + m)) and Space Complexity will be O ( (n + m) ). 这样,时间复杂度将为O(nlogn + mlogm +(n + m)),而空间复杂度将为O((n + m))。 If you think to merge the array first and then sort the merged array then the space complexity will be the same but time complexity will change to O((n + m)(log(n + m))), which will be definitely higher than first one .
如果您认为先合并数组然后对合并的数组进行排序,则空间复杂度将相同,但时间复杂度将更改为O((n + m)(log(n + m))),这肯定会更高比第一个。
array3
with size sum of array1.length
and array2.length
array3
,其大小之和为array1.length
和array2.length
System.arrayCopy()
to copy array1
to array3
starting at index 0
System.arrayCopy()
从索引0
开始将array1
复制到array3
System.arrayCopy()
to copy array2
to array3
starting at index array1.length
System.arrayCopy()
复制array2
至array3
开始于索引array1.length
array3
in the way you prefer eg Arrays.sort()
or any other algorithm
array3
进行排序,例如Arrays.sort()
或任何other algorithm
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.