6 |
7 | void merge(int a[], int l, int m, int r){
8 | int i, j, k;
9 | int n1 = m - l + 1;
10 | int n2 = r - m;
11 | int L[n1], R[n2];
12 |
13 | for(i = 0; i < n1; i++){
14 | L[i] = a[l + i];
15 | }
16 |
17 | for(j = 0; j < n2; j++){
18 | R[j] = a[m + 1 + j];
19 | }
20 |
21 | i = 0;
22 | j = 0;
23 | k = l;
24 |
25 | while(i < n1 && j < n2){
26 | if(L[i] <= R[j]){
27 | a[k] = L[i];
28 | i++;
29 | }else{
30 | a[k] = R[j];
31 | j++;
32 | }
33 | k++;
34 | }
35 |
36 | while(i < n1){
37 | a[k] = L[i];
38 | i++;
39 | k++;
40 | }
41 |
42 | while(j < n2){
43 | a[k] = R[j];
44 | j++;
45 | k++;
46 | }
47 | }
48 |
49 | void merge_sort(int a[], int l, int r){
50 | if(l < r){
51 | int m = l + (r - l) / 2;
52 |
53 | merge_sort(a, l, m);
54 | merge_sort(a, m + 1, r);
55 |
56 | merge(a, l, m, r);
57 | }
58 | }
59 |
60 | int main(void){
61 | int a[] = {5, 2, 4, 6, 1, 3};
62 | int n = 6;
63 | int i;
64 |
65 | merge_sort(a, 0, n - 1);
66 |
67 | for(i = 0; i < n; i++){
68 | printf("%d ", a[i]);
69 | }
70 |
71 | return 0;
72 | }
73 |
--------------------------------------------------------------------------------
/README.md:
--------------------------------------------------------------------------------
1 |
2 | Data Structures with C
3 |
4 |
5 | Welcome to the repository for the Data Structures with C course at UTD-CSVTU ! Here, you'll find resources, lab files, class codes, and more to help you excel in your studies of data structures.
6 |
7 |
8 |
9 |
10 |
11 | ## Table of Contents
12 | - [Folders](#folders)
13 | - [Lab Files](#lab-files)
14 | - [Unit-wise Folders](#unit-wise-folders)
15 | - [Quizzes](#quizzes)
16 | - [Faculties](#faculties)
17 | - [Lectures](#lectures)
18 | - [Labs](#labs)
19 |
20 |
21 | ## Folders
22 |
23 | ### Lab Files
24 | In this folder, you'll find all the lab files and codes related to the practical sessions conducted in our labs. These resources are invaluable for hands-on learning and experimentation.
25 |
26 | ### Unit-wise Folders
27 | Here, you'll discover unit-specific notes, class codes, and concept implementations organized neatly into individual folders. These materials will aid in your understanding and mastery of each topic covered in the course.
28 |
29 | ### Quizzes
30 | Prepare for casual class tests by accessing question papers uploaded here. These quizzes, provided by our esteemed faculty, offer an opportunity to assess your knowledge and comprehension of the subject matter.
31 |
32 |
33 | ## Faculties
34 |
35 | ### Lectures
36 | - **Dr. JP Patra**\
37 | Dr. Patra leads the lectures for this course, bringing a wealth of knowledge and experience to the classroom. His expertise ensures that you receive comprehensive instruction and guidance throughout your learning journey.
38 |
39 | ### Labs
40 | - **Mr. Abhinaw Jagtap**\
41 | Mr. Jagtap oversees the practical sessions in the lab, offering valuable insights and assistance as you engage with data structures firsthand. His support ensures a conducive environment for exploration and learning.
42 |
43 |
--------------------------------------------------------------------------------
/Unit_2/Searching_Techniques/Binary_Search.c:
--------------------------------------------------------------------------------
1 | // Implementing binary search in C
2 |
3 | #include
4 |
5 | int binarySearch(int arr[], int size, int target) {
6 | int left = 0;
7 | int right = size - 1;
8 | while (left <= right) {
9 | int mid = left + (right - left) / 2;
10 | if (arr[mid] == target) {
11 | return mid;
12 | }
13 | if (arr[mid] < target) {
14 | left = mid + 1;
15 | } else {
16 | right = mid - 1;
17 | }
18 | }
19 | return -1;
20 | }
21 |
22 | int main() {
23 | int array[] = {2, 4, 5, 7, 9, 12, 14};
24 | int target = 9;
25 | int size = sizeof(array) / sizeof(array[0]);
26 | int result = binarySearch(array, size, target);
27 | if (result != -1) {
28 | printf("Element found at index: %d\n", result);
29 | } else {
30 | printf("Element not found in the array\n");
31 | }
32 | return 0;
33 | }
34 |
35 | // another way to implement binary search
36 | // #include
37 |
38 | // int binarySearch(int arr[], int left, int right, int target) {
39 | // while (left <= right) {
40 | // int mid = left + (right - left) / 2;
41 |
42 | // // Check if target is present at mid
43 | // if (arr[mid] == target) {
44 | // return mid;
45 | // }
46 |
47 | // // If target greater, ignore left half
48 | // if (arr[mid] < target) {
49 | // left = mid + 1;
50 | // }
51 | // // If target is smaller, ignore right half
52 | // else {
53 | // right = mid - 1;
54 | // }
55 | // }
56 |
57 | // // Target not found
58 | // return -1;
59 | // }
60 |
61 | // int main() {
62 | // int n;
63 | // printf("Enter the size of the array: ");
64 | // scanf("%d", &n);
65 | // int array[n];
66 | // printf("Enter the elements of the array: ");
67 | // for (int i = 0; i < n; i++) {
68 | // scanf("%d", &array[i]);
69 | // }
70 | // int target;
71 | // printf("Enter the target element: ");
72 | // scanf("%d", &target);
73 |
74 | // int size = sizeof(array) / sizeof(array[0]);
75 | // int result = binarySearch(array, 0, size - 1, target);
76 | // if (result != -1) {
77 | // printf("Element found at index: %d\n", result);
78 | // } else {
79 | // printf("Element not found in the array\n");
80 | // }
81 | // return 0;
82 | // }
83 |
84 | // another way to implement binary search
85 | // #include
86 |
87 | // int binarySearch(int arr[], int size, int target) {
88 | // int left = 0;
89 | // int right = size - 1;
90 | // while (left <= right) {
91 | // int mid = left + (right - left) / 2;
92 | // if (arr[mid] == target) {
93 | // return mid;
94 | // }
95 | // if (arr[mid] < target) {
96 | // left = mid + 1;
97 | // } else {
98 | // right = mid - 1;
99 | // }
100 | // }
101 | // return -1;
102 | // }
103 |
104 | // int main() {
105 | // int array[] = {2, 4, 5, 7, 9, 12, 14};
106 | // int target = 9;
107 | // int size = sizeof(array) / sizeof(array[0]);
108 | // int result = binarySearch(array, size, target);
109 | // if (result != -1) {
110 | // printf("Element found at index: %d\n", result);
111 | // } else {
112 | // printf("Element not found in the array\n");
113 | // }
114 | // return 0;
115 | // }
116 |
--------------------------------------------------------------------------------