给定一个整数数组,其前半部分和后半部分均已排序。任务是将两个排序后的数组合并为一个排序后的数组。
例子:
Input : A[] = { 2, 3, 8, -1, 7, 10 }
Output : -1, 2, 3, 7, 8, 10
Input : A[] = {-4, 6, 9, -1, 3 }
Output : -4, -1, 3, 6, 9
一个简单的解决方案是对数组进行排序。
下面是上述方法的实现:
C++
// C++ program to Merge two sorted halves of
// array Into Single Sorted Array
#include
using namespace std;
void mergeTwoHalf(int A[], int n)
{
// Sort the given array using sort STL
sort(A, A + n);
}
// Driver code
int main()
{
int A[] = { 2, 3, 8, -1, 7, 10 };
int n = sizeof(A) / sizeof(A[0]);
mergeTwoHalf(A, n);
// Print sorted Array
for (int i = 0; i < n; i++)
cout << A[i] << " ";
return 0;
}
Java
// Java program to Merge two sorted halves of
// array Into Single Sorted Array
import java.io.*;
import java.util.*;
class GFG {
static void mergeTwoHalf(int[] A, int n)
{
// Sort the given array using sort STL
Arrays.sort(A);
}
// Driver code
static public void main(String[] args)
{
int[] A = { 2, 3, 8, -1, 7, 10 };
int n = A.length;
mergeTwoHalf(A, n);
// Print sorted Array
for (int i = 0; i < n; i++)
System.out.print(A[i] + " ");
}
}
// This code is contributed by vt_m .
Python
# Python program to Merge two sorted
# halves of array Into Single Sorted Array
def mergeTwoHalf(A, n):
# Sort the given array using sort STL
A.sort()
# Driver Code
if __name__ == '__main__':
A = [2, 3, 8, -1, 7, 10]
n = len(A)
mergeTwoHalf(A, n)
# Print sorted Array
for i in range(n):
print(A[i], end=" ")
# This code is contributed by 29AjayKumar
C#
// C# program to Merge two sorted halves of
// array Into Single Sorted Array
using System;
class GFG {
static void mergeTwoHalf(int[] A, int n)
{
// Sort the given array using sort STL
Array.Sort(A);
}
// Driver code
static public void Main()
{
int[] A = { 2, 3, 8, -1, 7, 10 };
int n = A.Length;
mergeTwoHalf(A, n);
// Print sorted Array
for (int i = 0; i < n; i++)
Console.Write(A[i] + " ");
}
}
// This code is contributed by vt_m .
PHP
C++
// C++ program to Merge Two Sorted Halves Of
// Array Into Single Sorted Array
#include
using namespace std;
// Merge two sorted halves of Array into single
// sorted array
void mergeTwoHalf(int A[], int n)
{
int half_i = 0; // starting index of second half
// Temp Array store sorted resultant array
int temp[n];
// First Find the point where array is divide
// into two half
for (int i = 0; i < n - 1; i++) {
if (A[i] > A[i + 1]) {
half_i = i + 1;
break;
}
}
// If Given array is all-ready sorted
if (half_i == 0)
return;
// Merge two sorted arrays in single sorted array
int i = 0, j = half_i, k = 0;
while (i < half_i && j < n) {
if (A[i] < A[j])
temp[k++] = A[i++];
else
temp[k++] = A[j++];
}
// Copy the remaining elements of A[i to half_! ]
while (i < half_i)
temp[k++] = A[i++];
// Copy the remaining elements of A[ half_! to n ]
while (j < n)
temp[k++] = A[j++];
for (int i = 0; i < n; i++)
A[i] = temp[i];
}
// Driver code
int main()
{
int A[] = { 2, 3, 8, -1, 7, 10 };
int n = sizeof(A) / sizeof(A[0]);
mergeTwoHalf(A, n);
// Print sorted Array
for (int i = 0; i < n; i++)
cout << A[i] << " ";
return 0;
}
Java
// Java program to Merge Two Sorted Halves Of
// Array Into Single Sorted Array
import java.io.*;
class GFG {
// Merge two sorted halves of Array
// into single sorted array
static void mergeTwoHalf(int[] A, int n)
{
int half_i = 0; // starting index of second half
int i;
// Temp Array store sorted resultant array
int[] temp = new int[n];
// First Find the point where array is divide
// into two half
for (i = 0; i < n - 1; i++) {
if (A[i] > A[i + 1]) {
half_i = i + 1;
break;
}
}
// If Given array is all-ready sorted
if (half_i == 0)
return;
// Merge two sorted arrays in single sorted array
i = 0;
int j = half_i;
int k = 0;
while (i < half_i && j < n) {
if (A[i] < A[j])
temp[k++] = A[i++];
else
temp[k++] = A[j++];
}
// Copy the remaining elements of A[i to half_! ]
while (i < half_i)
temp[k++] = A[i++];
// Copy the remaining elements of A[ half_! to n ]
while (j < n)
temp[k++] = A[j++];
for (i = 0; i < n; i++)
A[i] = temp[i];
}
// Driver code
static public void main(String[] args)
{
int[] A = { 2, 3, 8, -1, 7, 10 };
int n = A.length;
mergeTwoHalf(A, n);
// Print sorted Array
for (int i = 0; i < n; i++)
System.out.print(A[i] + " ");
}
}
// This code is contributed by vt_m .
Python3
# Python3 program to Merge Two Sorted Halves Of
# Array Into Single Sorted Array
# Merge two sorted halves of Array into single
# sorted array
def mergeTwoHalf(A, n):
# Starting index of second half
half_i = 0
# Temp Array store sorted resultant array
temp = [0 for i in range(n)]
# First Find the point where array is
# divide into two half
for i in range(n - 1):
if (A[i] > A[i + 1]):
half_i = i + 1
break
# If Given array is all-ready sorted
if (half_i == 0):
return
# Merge two sorted arrays in single
# sorted array
i = 0
j = half_i
k = 0
while (i < half_i and j < n):
if (A[i] < A[j]):
temp[k] = A[i]
k += 1
i += 1
else:
temp[k] = A[j]
k += 1
j += 1
# Copy the remaining elements of A[i to half_! ]
while i < half_i:
temp[k] = A[i]
k += 1
i += 1
# Copy the remaining elements of A[ half_! to n ]
while (j < n):
temp[k] = A[j]
k += 1
j += 1
for i in range(n):
A[i] = temp[i]
# Driver code
A = [ 2, 3, 8, -1, 7, 10 ]
n = len(A)
mergeTwoHalf(A, n)
# Print sorted Array
print(*A, sep = ' ')
# This code is contributed by avanitrachhadiya2155
C#
// C# program to Merge Two Sorted Halves Of
// Array Into Single Sorted Array
using System
class GFG {
// Merge two sorted halves of Array
// into single sorted array
static void mergeTwoHalf(int[] A, int n)
{
int half_i = 0
// starting index of second half
int i
// Temp Array store sorted resultant array
int[] temp
= new int[n]
// First Find the point where array is divide
// into two half
for (i = 0 i < n - 1 i++)
{
if (A[i] > A[i + 1]) {
half_i = i + 1 break
}
}
// If Given array is all-ready sorted
if (half_i == 0)
return
// Merge two sorted arrays in single sorted
// array
i = 0 int j = half_i int k
= 0 while (i < half_i & &j < n)
{
if (A[i] < A[j])
temp[k++] = A[i++] else temp[k++]
= A[j++]
}
// Copy the remaining elements of A[i to half_!]
while (i < half_i)
temp[k++] = A[i++]
// Copy the remaining elements of A[half_!
// to n]
while (j < n) temp[k++]
= A[j++]
for (i = 0 i < n i++) A[i]
= temp[i]
}
// Driver code
static public void Main()
{
int[] A
= { 2,
3,
8,
-1,
7,
10 } int n
= A.Length mergeTwoHalf(A, n)
// Print sorted Array
for (int i = 0 i < n i++)
Console.Write(A[i] + " ")
}
}
// This code is contributed by vt_m .
Javascript
C++
// C++ program to Merge Two Sorted Halves Of
// Array Into Single Sorted Array
#include
using namespace std;
void SortTwoHalfSorted(int A[], int n)
{
int i = 0;
int j = n / 2;
// loop until end of array
while (j < n) {
// if two pointer is equal then go
// to next element of second half.
if (i == j)
j++;
// if element of first half is bigger
// than element of second half swap two
// elements and go next element of first half.
if (j < n && A[i] > A[j]) {
swap(A[i], A[j]);
}
i++;
}
}
// Driver code
int main()
{
int A[] = { 2, 3, 8, -1, 7, 10 };
int n = sizeof(A) / sizeof(A[0]);
SortTwoHalfSorted(A, n);
// Print sorted Array
for (int i = 0; i < n; i++)
cout << A[i] << " ";
return 0;
}
Java
// Java program to Merge Two Sorted Halves Of
// Array Into Single Sorted Array
import java.io.*;
import java.util.*;
class GFG {
public static void sortTwoHalfs(int a[], int n)
{
int i = 0;
int j = n / 2;
// loop until end of array
while (j < n) {
// if two pointer is equal then go
// to next element of second half.
if (i == j)
j++;
// if element of first half is bigger
// than element of second half swap two
// elements and go next element of first half.
if (j < n && a[i] > a[j]) {
// Swap elements
int temp = a[i];
a[i] = a[j];
a[j] = temp;
}
i++;
}
}
// Driver Code
public static void main(String[] args)
{
int a[] = { 2, 3, 8, -1, 7, 10 };
int n = a.length;
sortTwoHalfs(a, n); // Call func. to sort array.
// Print sorted array
for (int i = 0; i < n; i++) {
System.out.print(a[i] + " ");
}
}
}
Python3
# Python3 program to Merge Two Sorted
# Halves Of Array Into Single Sorted Array
def SortTwoHalfSorted(A, n):
i = 0
j = n // 2
# Loop until end of array
while (j < n):
# If two pointer is equal then go
# to next element of second half.
if (i == j):
j += 1
# If element of first half is bigger
# than element of second half swap two
# elements and go next element of first half
if (j < n and A[i] > A[j]):
A[i], A[j] = A[j], A[i]
i += 1
# Driver code
A = [ 2, 3, 8, -1, 7, 10 ]
n = len(A)
SortTwoHalfSorted(A, n)
# Print sorted Array
for i in range(n):
print(A[i], end = " ")
# This code is contributed by divyesh072019
C#
// C# program to Merge Two Sorted Halves Of
// Array Into Single Sorted Array
using System;
using System.Collections.Generic;
class GFG{
static void sortTwoHalfs(int[] a, int n)
{
int i = 0;
int j = n / 2;
// Loop until end of array
while (j < n)
{
// If two pointer is equal then go
// to next element of second half.
if (i == j)
j++;
// If element of first half is bigger
// than element of second half swap two
// elements and go next element of first half.
if (j < n && a[i] > a[j])
{
// Swap elements
int temp = a[i];
a[i] = a[j];
a[j] = temp;
}
i++;
}
}
// Driver code
static void Main()
{
int[] a = { 2, 3, 8, -1, 7, 10 };
int n = a.Length;
// Call func. to sort array.
sortTwoHalfs(a, n);
// Print sorted array
for(int i = 0; i < n; i++)
{
Console.Write(a[i] + " ");
}
}
}
// This code is contributed by divyeshrabadiya07
Javascript
输出
-1 2 3 7 8 10
时间复杂度O(nlogn)||使用快速排序或合并排序对给定数组进行排序
一种有效的解决方案是使用辅助阵列的一半。现在,整个过程与合并排序的合并函数相同。
下面是上述方法的实现:
C++
// C++ program to Merge Two Sorted Halves Of
// Array Into Single Sorted Array
#include
using namespace std;
// Merge two sorted halves of Array into single
// sorted array
void mergeTwoHalf(int A[], int n)
{
int half_i = 0; // starting index of second half
// Temp Array store sorted resultant array
int temp[n];
// First Find the point where array is divide
// into two half
for (int i = 0; i < n - 1; i++) {
if (A[i] > A[i + 1]) {
half_i = i + 1;
break;
}
}
// If Given array is all-ready sorted
if (half_i == 0)
return;
// Merge two sorted arrays in single sorted array
int i = 0, j = half_i, k = 0;
while (i < half_i && j < n) {
if (A[i] < A[j])
temp[k++] = A[i++];
else
temp[k++] = A[j++];
}
// Copy the remaining elements of A[i to half_! ]
while (i < half_i)
temp[k++] = A[i++];
// Copy the remaining elements of A[ half_! to n ]
while (j < n)
temp[k++] = A[j++];
for (int i = 0; i < n; i++)
A[i] = temp[i];
}
// Driver code
int main()
{
int A[] = { 2, 3, 8, -1, 7, 10 };
int n = sizeof(A) / sizeof(A[0]);
mergeTwoHalf(A, n);
// Print sorted Array
for (int i = 0; i < n; i++)
cout << A[i] << " ";
return 0;
}
Java
// Java program to Merge Two Sorted Halves Of
// Array Into Single Sorted Array
import java.io.*;
class GFG {
// Merge two sorted halves of Array
// into single sorted array
static void mergeTwoHalf(int[] A, int n)
{
int half_i = 0; // starting index of second half
int i;
// Temp Array store sorted resultant array
int[] temp = new int[n];
// First Find the point where array is divide
// into two half
for (i = 0; i < n - 1; i++) {
if (A[i] > A[i + 1]) {
half_i = i + 1;
break;
}
}
// If Given array is all-ready sorted
if (half_i == 0)
return;
// Merge two sorted arrays in single sorted array
i = 0;
int j = half_i;
int k = 0;
while (i < half_i && j < n) {
if (A[i] < A[j])
temp[k++] = A[i++];
else
temp[k++] = A[j++];
}
// Copy the remaining elements of A[i to half_! ]
while (i < half_i)
temp[k++] = A[i++];
// Copy the remaining elements of A[ half_! to n ]
while (j < n)
temp[k++] = A[j++];
for (i = 0; i < n; i++)
A[i] = temp[i];
}
// Driver code
static public void main(String[] args)
{
int[] A = { 2, 3, 8, -1, 7, 10 };
int n = A.length;
mergeTwoHalf(A, n);
// Print sorted Array
for (int i = 0; i < n; i++)
System.out.print(A[i] + " ");
}
}
// This code is contributed by vt_m .
Python3
# Python3 program to Merge Two Sorted Halves Of
# Array Into Single Sorted Array
# Merge two sorted halves of Array into single
# sorted array
def mergeTwoHalf(A, n):
# Starting index of second half
half_i = 0
# Temp Array store sorted resultant array
temp = [0 for i in range(n)]
# First Find the point where array is
# divide into two half
for i in range(n - 1):
if (A[i] > A[i + 1]):
half_i = i + 1
break
# If Given array is all-ready sorted
if (half_i == 0):
return
# Merge two sorted arrays in single
# sorted array
i = 0
j = half_i
k = 0
while (i < half_i and j < n):
if (A[i] < A[j]):
temp[k] = A[i]
k += 1
i += 1
else:
temp[k] = A[j]
k += 1
j += 1
# Copy the remaining elements of A[i to half_! ]
while i < half_i:
temp[k] = A[i]
k += 1
i += 1
# Copy the remaining elements of A[ half_! to n ]
while (j < n):
temp[k] = A[j]
k += 1
j += 1
for i in range(n):
A[i] = temp[i]
# Driver code
A = [ 2, 3, 8, -1, 7, 10 ]
n = len(A)
mergeTwoHalf(A, n)
# Print sorted Array
print(*A, sep = ' ')
# This code is contributed by avanitrachhadiya2155
C#
// C# program to Merge Two Sorted Halves Of
// Array Into Single Sorted Array
using System
class GFG {
// Merge two sorted halves of Array
// into single sorted array
static void mergeTwoHalf(int[] A, int n)
{
int half_i = 0
// starting index of second half
int i
// Temp Array store sorted resultant array
int[] temp
= new int[n]
// First Find the point where array is divide
// into two half
for (i = 0 i < n - 1 i++)
{
if (A[i] > A[i + 1]) {
half_i = i + 1 break
}
}
// If Given array is all-ready sorted
if (half_i == 0)
return
// Merge two sorted arrays in single sorted
// array
i = 0 int j = half_i int k
= 0 while (i < half_i & &j < n)
{
if (A[i] < A[j])
temp[k++] = A[i++] else temp[k++]
= A[j++]
}
// Copy the remaining elements of A[i to half_!]
while (i < half_i)
temp[k++] = A[i++]
// Copy the remaining elements of A[half_!
// to n]
while (j < n) temp[k++]
= A[j++]
for (i = 0 i < n i++) A[i]
= temp[i]
}
// Driver code
static public void Main()
{
int[] A
= { 2,
3,
8,
-1,
7,
10 } int n
= A.Length mergeTwoHalf(A, n)
// Print sorted Array
for (int i = 0 i < n i++)
Console.Write(A[i] + " ")
}
}
// This code is contributed by vt_m .
Java脚本
输出
-1 2 3 7 8 10
时间复杂度:O(n)
参考:https://www.careercup.com/question?id = 8412257
另一个有效的解决方案是使用两个指针i和j,并比较a [i]和a [j]。使用合并需要的空间为O(n),但需要的空间为O(1)。下面是实现:
C++
// C++ program to Merge Two Sorted Halves Of
// Array Into Single Sorted Array
#include
using namespace std;
void SortTwoHalfSorted(int A[], int n)
{
int i = 0;
int j = n / 2;
// loop until end of array
while (j < n) {
// if two pointer is equal then go
// to next element of second half.
if (i == j)
j++;
// if element of first half is bigger
// than element of second half swap two
// elements and go next element of first half.
if (j < n && A[i] > A[j]) {
swap(A[i], A[j]);
}
i++;
}
}
// Driver code
int main()
{
int A[] = { 2, 3, 8, -1, 7, 10 };
int n = sizeof(A) / sizeof(A[0]);
SortTwoHalfSorted(A, n);
// Print sorted Array
for (int i = 0; i < n; i++)
cout << A[i] << " ";
return 0;
}
Java
// Java program to Merge Two Sorted Halves Of
// Array Into Single Sorted Array
import java.io.*;
import java.util.*;
class GFG {
public static void sortTwoHalfs(int a[], int n)
{
int i = 0;
int j = n / 2;
// loop until end of array
while (j < n) {
// if two pointer is equal then go
// to next element of second half.
if (i == j)
j++;
// if element of first half is bigger
// than element of second half swap two
// elements and go next element of first half.
if (j < n && a[i] > a[j]) {
// Swap elements
int temp = a[i];
a[i] = a[j];
a[j] = temp;
}
i++;
}
}
// Driver Code
public static void main(String[] args)
{
int a[] = { 2, 3, 8, -1, 7, 10 };
int n = a.length;
sortTwoHalfs(a, n); // Call func. to sort array.
// Print sorted array
for (int i = 0; i < n; i++) {
System.out.print(a[i] + " ");
}
}
}
Python3
# Python3 program to Merge Two Sorted
# Halves Of Array Into Single Sorted Array
def SortTwoHalfSorted(A, n):
i = 0
j = n // 2
# Loop until end of array
while (j < n):
# If two pointer is equal then go
# to next element of second half.
if (i == j):
j += 1
# If element of first half is bigger
# than element of second half swap two
# elements and go next element of first half
if (j < n and A[i] > A[j]):
A[i], A[j] = A[j], A[i]
i += 1
# Driver code
A = [ 2, 3, 8, -1, 7, 10 ]
n = len(A)
SortTwoHalfSorted(A, n)
# Print sorted Array
for i in range(n):
print(A[i], end = " ")
# This code is contributed by divyesh072019
C#
// C# program to Merge Two Sorted Halves Of
// Array Into Single Sorted Array
using System;
using System.Collections.Generic;
class GFG{
static void sortTwoHalfs(int[] a, int n)
{
int i = 0;
int j = n / 2;
// Loop until end of array
while (j < n)
{
// If two pointer is equal then go
// to next element of second half.
if (i == j)
j++;
// If element of first half is bigger
// than element of second half swap two
// elements and go next element of first half.
if (j < n && a[i] > a[j])
{
// Swap elements
int temp = a[i];
a[i] = a[j];
a[j] = temp;
}
i++;
}
}
// Driver code
static void Main()
{
int[] a = { 2, 3, 8, -1, 7, 10 };
int n = a.Length;
// Call func. to sort array.
sortTwoHalfs(a, n);
// Print sorted array
for(int i = 0; i < n; i++)
{
Console.Write(a[i] + " ");
}
}
}
// This code is contributed by divyeshrabadiya07
Java脚本
输出
-1 2 3 7 8 10
时间复杂度:O(n)和空间:O(1)