01 - Ca2 - Bcac 303
01 - Ca2 - Bcac 303
INSERTION OF ARRAY
An array is a collection of items stored at contiguous memory locations. In this
article, we will see how to insert an element in an array in C.
Given an array arr of size n, this article tells how to insert an element x in this
array arr at a specific position pos.
Insertion Sort Algorithm
To sort an array of size N in ascending order:
• Iterate from arr[1] to arr[N] over the array.
• Compare the current element (key) to its predecessor.
• If the key element is smaller than its predecessor, compare it to the
elements before. Move the greater elements one position up to make
space for the swapped element.
#include <bits/stdc++.h>
// insertion sort
int i, key, j;
key = arr[i];
j = i - 1;
// current position
{
arr[j + 1] = arr[j];
j = j - 1;
arr[j + 1] = key;
// of size n
int i;
// Driver code
int main()
insertionSort(arr, N);
printArray(arr, N);
return 0;
DELETION OF ARRAY
Algorithm to Delete an element from an Array:
• Step 04: [Move ith element backward (left). ] set a[i] = a[i+1]
Implementation in C:
Output:
1
2The array elements before deletion operation:
3a[0] = 2
4a[1] = 4
a[2] = 6
5
a[3] = 8
6a[4] = 12
7Enter the position from where you wish to delete the element: 3
8The array elements after deletion operation:
9a[0] = 2
10a[1] = 4
11a[2] = 8
12a[3] = 12
• Set j to 1
• If j > n, jump to step 7
• If X[j] == i, jump to step 6
• Then, increment j by 1 i.e. j = j+1
• Go back to step 2
• Display the element i which is found at particular index i, then jump to step
8
• Display element not found in the set of input elements.
• Exit/End
#include <stdio.h>
int main(void)
{
int arr[] = { 10, 20, 30, 40, 50, 100, 0 };
int key = 100;
int size = 10;
int res = LINEAR_SEARCH(arr, size, key);
if (res == -1)
printf("ELEMENT NOT FOUND!!");
else
printf("Item is present at index %d", res);
return 0;
}
Output:
1. #include <iostream>
2. using namespace std;
3. int binarySearch(int a[], int beg, int end, int val)
4. {
5. int mid;
6. if(end >= beg)
7. {
8. mid = (beg + end)/2;
9. /* if the item to be searched is present at middle */
10. if(a[mid] == val)
11. {
12. return mid+1;
13. }
14. /* if the item to be searched is smaller than middle, then it can only be in l
eft subarray */
15. else if(a[mid] < val)
16. {
17. return binarySearch(a, mid+1, end, val);
18. }
19. /* if the item to be searched is greater than middle, then it can only be in rig
ht subarray */
20. else
21. {
22. return binarySearch(a, beg, mid-1, val);
23. }
24. }
25. return -1;
26. }
27. int main() {
28. int a[] = {10, 12, 24, 29, 39, 40, 51, 56, 70}; // given array
29. int val = 51; // value to be searched
30. int n = sizeof(a) / sizeof(a[0]); // size of array
31. int res = binarySearch(a, 0, n-1, val); // Store result
32. cout<<"The elements of the array are - ";
33. for (int i = 0; i < n; i++)
34. cout<<a[i]<<" ";
35. cout<<"\nElement to be searched is - "<<val;
36. if (res == -1)
37. cout<<"\nElement is not present in the array";
38. else
39. cout<<"\nElement is present at "<<res<<" position of array";
40. return 0;
41. }
OUTPUT:
Output
Sorted array:
1 2 4 5 8
output
Sorted array:
11 12 22 25 64
\\
Step 2. Now pick the new element which you want to insert.
Step 3. Compare the new picked element with the sorted element of an
array.
OUTPUT:
MERGE SORT ALGORITHM
In the following algorithm, arr is the given array, beg is the starting element, and end is
the last element of the array.
1. #include <stdio.h>
2.
3. /* Function to merge the subarrays of a[] */
4. void merge(int a[], int beg, int mid, int end)
5. {
6. int i, j, k;
7. int n1 = mid - beg + 1;
8. int n2 = end - mid;
9.
10. int LeftArray[n1], RightArray[n2]; //temporary arrays
11.
12. /* copy data to temp arrays */
13. for (int i = 0; i < n1; i++)
14. LeftArray[i] = a[beg + i];
15. for (int j = 0; j < n2; j++)
16. RightArray[j] = a[mid + 1 + j];
17.
18. i = 0; /* initial index of first sub-array */
19. j = 0; /* initial index of second sub-array */
20. k = beg; /* initial index of merged sub-array */
21.
22. while (i < n1 && j < n2)
23. {
24. if(LeftArray[i] <= RightArray[j])
25. {
26. a[k] = LeftArray[i];
27. i++;
28. }
29. else
30. {
31. a[k] = RightArray[j];
32. j++;
33. }
34. k++;
35. }
36. while (i<n1)
37. {
38. a[k] = LeftArray[i];
39. i++;
40. k++;
41. }
42.
43. while (j<n2)
44. {
45. a[k] = RightArray[j];
46. j++;
47. k++;
48. }
49. }
50.
51. void mergeSort(int a[], int beg, int end)
52. {
53. if (beg < end)
54. {
55. int mid = (beg + end) / 2;
56. mergeSort(a, beg, mid);
57. mergeSort(a, mid + 1, end);
58. merge(a, beg, mid, end);
59. }
60. }
61.
62. /* Function to print the array */
63. void printArray(int a[], int n)
64. {
65. int i;
66. for (i = 0; i < n; i++)
67. printf("%d ", a[i]);
68. printf("\n");
69. }
70.
71. int main()
72. {
73. int a[] = { 12, 31, 25, 8, 32, 17, 40, 42 };
74. int n = sizeof(a) / sizeof(a[0]);
75. printf("Before sorting array elements are - \n");
76. printArray(a, n);
77. mergeSort(a, 0, n - 1);
78. printf("After sorting array elements are - \n");
79. printArray(a, n);
80. return 0;
81. }
OUTPUT:
Pivot
1. Picks an element called the "pivot".
Partition
2. Rearrange the array elements in such a way that the all values lesser than the
pivot should come before the pivot and all the values greater than the pivot should
come after it.
This method is called partitioning the array. At the end of the partition function, the
pivot element will be placed at its sorted position.
Recursive
3. Do the above process recursively to all the sub-arrays and sort the elements.
Base Case
If the array has zero or one element, there is no need to call the partition method.
So we need to stop the recursive call when the array size is less than or equal to 1.
1. #include <iostream>
2.
3. using namespace std;
4.
5. /* function that consider last element as pivot,
6. place the pivot at its exact position, and place
7. smaller elements to left of pivot and greater
8. elements to right of pivot. */
9. int partition (int a[], int start, int end)
10. {
11. int pivot = a[end]; // pivot element
12. int i = (start - 1);
13.
14. for (int j = start; j <= end - 1; j++)
15. {
16. // If current element is smaller than the pivot
17. if (a[j] < pivot)
18. {
19. i++; // increment index of smaller element
20. int t = a[i];
21. a[i] = a[j];
22. a[j] = t;
23. }
24. }
25. int t = a[i+1];
26. a[i+1] = a[end];
27. a[end] = t;
28. return (i + 1);
29. }
30.
31. /* function to implement quick sort */
32. void quick(int a[], int start, int end) /* a[] = array to be sorted, start = Starting in
dex, end = Ending index */
33. {
34. if (start < end)
35. {
36. int p = partition(a, start, end); //p is the partitioning index
37. quick(a, start, p - 1);
38. quick(a, p + 1, end);
39. }
40. }
41.
42. /* function to print an array */
43. void printArr(int a[], int n)
44. {
45. int i;
46. for (i = 0; i < n; i++)
47. cout<<a[i]<< " ";
48. }
49. int main()
50. {
51. int a[] = { 23, 8, 28, 13, 18, 26 };
52. int n = sizeof(a) / sizeof(a[0]);
53. cout<<"Before sorting array elements are - \n";
54. printArr(a, n);
55. quick(a, 0, n - 1);
56. cout<<"\nAfter sorting array elements are - \n";
57. printArr(a, n);
58.
59. return 0;
60. }
OUTPUT: