0% found this document useful (0 votes)
5 views7 pages

C Programming: Array Sorting & BST

The document contains several C programming modules that demonstrate various algorithms and data structures, including sorting arrays of strings, handling 1D arrays, reversing arrays, and implementing a binary search tree with insertion. It also includes a function to remove duplicates from a sorted array. Each module provides code snippets and outlines the main functions and their usage.

Uploaded by

jagaththarinis
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views7 pages

C Programming: Array Sorting & BST

The document contains several C programming modules that demonstrate various algorithms and data structures, including sorting arrays of strings, handling 1D arrays, reversing arrays, and implementing a binary search tree with insertion. It also includes a function to remove duplicates from a sorted array. Each module provides code snippets and outlines the main functions and their usage.

Uploaded by

jagaththarinis
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

Module 4:

1. SORTING ARRAY OF STRINGS


#include<string.h>
#include<stdio.h>
#include<stdlib.h>
int lexicographic_sort(const char* a, const char* b){
return strcmp(a, b) > 0;
}

int lexicographic_sort_reverse(const char* a, const char* b){


return strcmp(a, b) <= 0;
}

int sort_by_number_of_distinct_characters(const char* a, const char* b){


int c1 = 0, c2 = 0;
int hsh1[26] = {0}, hsh2[26] = {0};
int n1 = strlen(a);
int n2 = strlen(b);
int i;

for(i = 0; i < n1; i++){


hsh1[a[i] - 'a'] = 1;
}

for(i = 0; i < n2; i++){


hsh2[b[i] - 'a'] = 1;
}

for( i = 0; i < 26; i++){


if(hsh1[i])
c1++;
if(hsh2[i])
c2++;
}
if( c1 != c2)
return c1 > c2;
else
return strcmp(a, b) > 0;

int sort_by_length(const char* a, const char* b){


//code
}

void string_sort(char** arr,const int len,int (*cmp_func)(const char* a, const


char* b))
{ int i;
for( i = 1; i < len; i++){
int j = i;
char* p = arr[i];
while(j > 0){
if((*cmp_func)(arr[j-1],p) > 0 )
arr[j] = arr[j-1];
else
break;
j--;
}
arr[j] = p;
}
}

int main()
{
int n,i;
scanf("%d", &n);

char** arr;
arr = (char**)malloc(n * sizeof(char*));

for(i = 0; i < n; i++){


*(arr + i) = malloc(1024 * sizeof(char));
scanf("%s", *(arr + i));
*(arr + i) = realloc(*(arr + i), strlen(*(arr + i)) + 1);
}

string_sort(arr, n, lexicographic_sort);
for(i = 0; i < n; i++)
printf("%s\n", arr[i]);
printf("\n");

string_sort(arr, n, lexicographic_sort_reverse);
for(i = 0; i < n; i++)
printf("%s\n", arr[i]);
printf("\n");

string_sort(arr, n, sort_by_length);
for(i = 0; i < n; i++)
printf("%s\n", arr[i]);
printf("\n");

string_sort(arr, n, sort_by_number_of_distinct_characters);
for(i = 0; i < n; i++)
printf("%s\n", arr[i]);
printf("\n");
}
2. 1D ARRAYS IN C

#include <stdio.h>
#include <stdlib.h>

int main()
{
//code
printf("%d\n", sum);

free(arr);

return 0;
}

3. Array Reversal
#include <stdio.h>
#include <stdlib.h>
int main() {
int n, arr[1000], i;
scanf("%d", &n);
//code
printf("\n");
return 0;
}

4. Binary Search Tree: Insertion


#include <stdio.h>
#include <string.h>
#include <math.h>
#include <stdlib.h>

struct node {

int data;
struct node *left;
struct node *right;

};

void preOrder( struct node *root) {

if( root == NULL )


return;
printf("%d ",root->data);
preOrder(root->left);
preOrder(root->right);

}
struct node* insert(struct node* root, int data) {
//code

int main() {

struct node* root = NULL;


int t;
int data;

scanf("%d", &t);

while(t-- > 0) {
scanf("%d", &data);
root = insert(root, data);
}

preOrder(root);
return 0;
}

Remove Duplicates from Sorted Array


#include <stdio.h>
// Function to remove duplicates from a sorted array
int removeDuplicates(int* nums, int numsSize) {
// Edge case: if the array is empty, no unique elements exist
if (numsSize == 0) {
return 0;
}
// k will track the index of the last unique element
int k = 1;
int i;
// Start from the second element (index 1)
for (i = 1; i < numsSize; i++) {
// If the current element is different from the previous one, it's unique
if (nums[i] != nums[i - 1]) {
// Place the unique element at position k
nums[k] = nums[i];
k++; // Increment k to track the number of unique elements
}
}
// Return the number of unique elements
return k;
}
// Main function to test the remove Duplicates function
int main() {
//code
printf("Array after removing duplicates: ");
for (i = 0; i < newSize; i++) {
printf("%d ", nums[i]);
}
printf("\n");
printf("Number of unique elements: %d\n", newSize);
return 0;
}

You might also like