fork download
  1. #include <bits/stdc++.h>
  2.  
  3. using namespace std;
  4.  
  5. // A function to merge the two half into a sorted data.
  6. void Merge(int *a, int low, int high, int mid)
  7. {
  8. // We have low to mid and mid+1 to high already sorted.
  9. int i, j, k, temp[high-low+1];
  10. i = low;
  11. k = 0;
  12. j = mid + 1;
  13.  
  14. // Merge the two parts into temp[].
  15. while (i <= mid && j <= high)
  16. {
  17. if (a[i] < a[j])
  18. {
  19. temp[k] = a[i];
  20. k++;
  21. i++;
  22. }
  23. else
  24. {
  25. temp[k] = a[j];
  26. k++;
  27. j++;
  28. }
  29. }
  30.  
  31. // Insert all the remaining values from i to mid into temp[].
  32. while (i <= mid)
  33. {
  34. temp[k] = a[i];
  35. k++;
  36. i++;
  37. }
  38.  
  39. // Insert all the remaining values from j to high into temp[].
  40. while (j <= high)
  41. {
  42. temp[k] = a[j];
  43. k++;
  44. j++;
  45. }
  46.  
  47.  
  48. // Assign sorted data stored in temp[] to a[].
  49. for (i = low; i <= high; i++)
  50. {
  51. a[i] = temp[i-low];
  52. }
  53. }
  54.  
  55. // A function to split array into two parts.
  56. void MergeSort(int *a, int low, int high)
  57. {
  58. int mid;
  59. if (low < high)
  60. {
  61. mid=(low+high)/2;
  62. // Split the data into two half.
  63. MergeSort(a, low, mid);
  64. MergeSort(a, mid+1, high);
  65.  
  66. // Merge them to get sorted output.
  67. Merge(a, low, high, mid);
  68. }
  69. }
  70.  
  71. int main()
  72. {
  73. int n, i;
  74. n = 1000000;
  75.  
  76. int arr[n];
  77. for(i = 0; i < n; i++)
  78. {
  79. arr[i] = rand();
  80. }
  81.  
  82. MergeSort(arr, 0, n-1);
  83.  
  84. // Printing the sorted data.
  85. cout<<"\nSorted Data ";
  86. for (i = 0; i < n; i++)
  87. cout<<arr[i]<<"\n";
  88.  
  89. return 0;
  90. }
Success #stdin #stdout 0.03s 25164KB
stdin
Standard input is empty
stdout
#include <bits/stdc++.h>
 
using namespace std;
 
// A function to merge the two half into a sorted data.
void Merge(int *a, int low, int high, int mid)
{
	// We have low to mid and mid+1 to high already sorted.
	int i, j, k, temp[high-low+1];
	i = low;
	k = 0;
	j = mid + 1;
 
	// Merge the two parts into temp[].
	while (i <= mid && j <= high)
	{
		if (a[i] < a[j])
		{
			temp[k] = a[i];
			k++;
			i++;
		}
		else
		{
			temp[k] = a[j];
			k++;
			j++;
		}
	}
 
	// Insert all the remaining values from i to mid into temp[].
	while (i <= mid)
	{
		temp[k] = a[i];
		k++;
		i++;
	}
 
	// Insert all the remaining values from j to high into temp[].
	while (j <= high)
	{
		temp[k] = a[j];
		k++;
		j++;
	}
 
 
	// Assign sorted data stored in temp[] to a[].
	for (i = low; i <= high; i++)
	{
		a[i] = temp[i-low];
	}
}
 
// A function to split array into two parts.
void MergeSort(int *a, int low, int high)
{
	int mid;
	if (low < high)
	{
		mid=(low+high)/2;
		// Split the data into two half.
		MergeSort(a, low, mid);
		MergeSort(a, mid+1, high);
 
		// Merge them to get sorted output.
		Merge(a, low, high, mid);
	}
}
 
int main()
{
	int n, i;
	n = 1000000;
 
	int arr[n];
	for(i = 0; i < n; i++)
	{
		arr[i] = rand();
	}
 
	MergeSort(arr, 0, n-1);
 
	// Printing the sorted data.
	cout<<"\nSorted Data ";
	for (i = 0; i < n; i++)
        cout<<arr[i]<<"\n";
 
	return 0;
}