-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path0088-Merge-sorted-array.cs
More file actions
67 lines (57 loc) · 1.67 KB
/
0088-Merge-sorted-array.cs
File metadata and controls
67 lines (57 loc) · 1.67 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
using System;
using System.Collections.Generic;
using System.Text;
namespace Solution._0088.Merge_sorted_array
{
public class _0088_Merge_sorted_array
{
public void Merge(int[] nums1, int m, int[] nums2, int n)
{
// Solution 1
int i, j, tmp, flag;
for (i = 0; i < n; i++)
nums1[i + m] = nums2[i];
//Array.Sort(nums1);
// bubble sort
for (i = nums1.Length - 1; i > 0; i--)
{
flag = 0;
for (j = 0; j < i; j++)
{
if (nums1[j] > nums1[j + 1])
{
// swap
tmp = nums1[j];
nums1[j] = nums1[j + 1];
nums1[j + 1] = tmp;
flag++;
}
}
if (flag == 0) break;
}
// Solution 2
//int i = m - 1, j = n - 1, index = m + n - 1;
//while (i >= 0 && j >= 0)
//{
// if (nums1[i] > nums2[j])
// {
// nums1[index] = nums1[i];
// i--;
// }
// else
// {
// nums1[index] = nums2[j];
// j--;
// }
// index--;
//}
//// No need to handle i >= 0 case. If it's the case, the remaining numbers are already in nums1.
//while (j >= 0)
//{
// nums1[index] = nums2[j];
// index--;
// j--;
//}
}
}
}