Skip to content

Commit 3dc2ecc

Browse files
author
Christian Bender
authored
Merge pull request TheAlgorithms#123 from agnimish/master
3 very useful codes added
2 parents 7efed28 + fade142 commit 3dc2ecc

File tree

3 files changed

+242
-0
lines changed

3 files changed

+242
-0
lines changed
Lines changed: 103 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,103 @@
1+
#include<stdio.h>
2+
#include<stdlib.h>
3+
struct node{
4+
int data;
5+
struct node *next;
6+
};
7+
8+
struct node *head1 = NULL;
9+
struct node *head2 = NULL;
10+
11+
///// MAIN ALGORITHMIC FUNCTION to MERGE the two input linked lists ///////
12+
13+
void merge()
14+
{
15+
struct node *temp1 = head1;
16+
struct node *temp2 = head2;
17+
18+
struct node *holder1 = NULL;
19+
struct node *holder2 = NULL;
20+
//Temporary pointer variables to store the address of next node of the two input linked list
21+
22+
while(temp1!=NULL && temp2!=NULL)
23+
{
24+
holder1 = temp1 -> next;
25+
//Storing the address of next node of first linked list
26+
temp1->next=temp2;
27+
//Making the first node of first linked list point to first node of second linked list
28+
29+
if(holder1!=NULL) {
30+
//Making the first node of second linked list point to second node of first linked list
31+
holder2 = temp2 -> next;
32+
temp2 -> next = holder1;
33+
}
34+
temp1=holder1;
35+
temp2=holder2;
36+
//Updating the address location of two pointer variables temp1 and temp2
37+
}
38+
}
39+
40+
void printlist(struct node *temp){
41+
printf("%d",temp->data);
42+
temp=temp->next;
43+
while(temp!=NULL){
44+
printf("->%d",temp->data);
45+
temp=temp->next;
46+
}
47+
printf("\n");
48+
}
49+
50+
int main()
51+
{
52+
// Linked List 1: 1->3->5->7 : Linked List 2: 2->4->6
53+
// making lists
54+
struct node *one = (struct node*)malloc(sizeof(struct node));
55+
struct node *two = (struct node*)malloc(sizeof(struct node));
56+
struct node *three = (struct node*)malloc(sizeof(struct node));
57+
struct node *four = (struct node*)malloc(sizeof(struct node));
58+
struct node *five = (struct node*)malloc(sizeof(struct node));
59+
struct node *six = (struct node*)malloc(sizeof(struct node));
60+
struct node *seven = (struct node*)malloc(sizeof(struct node));
61+
//Seven nodes are created
62+
63+
head1=one;
64+
head2=two;
65+
//head1 points to first node of first linked list
66+
//head2 points to first node of second linked list
67+
68+
one->data=1;
69+
one->next=three;
70+
71+
two->data=2;
72+
two->next=four;
73+
74+
three->data=3;
75+
three->next=five;
76+
77+
four->data=4;
78+
four->next=six;
79+
80+
five->data=5;
81+
five->next=seven;
82+
83+
six->data=6;
84+
six->next=NULL;
85+
//Last node of second input linked list
86+
87+
seven->data=7;
88+
seven->next=NULL;
89+
//Last node of first input linked list
90+
91+
printf("Linked List 1: ");
92+
printlist(head1);
93+
printf("\nLinked List 2: ");
94+
printlist(head2);
95+
96+
//Merging the two linked list into single linked list
97+
merge();
98+
99+
printf("\nMerged Linked List: ");
100+
printlist(head1); //list one has been modified
101+
102+
return 0;
103+
}

Searches/modifiedBinarySearch.c

Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
#include<stdio.h>
2+
3+
int n,m; //size of the matrix
4+
5+
// This function does Binary search for x in i-th row from j_low to j_high.
6+
void binarySearch(int mat[n][m], int i, int j_low,int j_high, int x)
7+
{
8+
while (j_low <= j_high)
9+
{
10+
int j_mid = (j_low + j_high) / 2;
11+
12+
// Element found
13+
if (mat[i][j_mid] == x){
14+
printf("Found at (%d,%d)\n",i,j_mid);
15+
return ;
16+
}
17+
else if (mat[i][j_mid] > x)
18+
j_high = j_mid - 1;
19+
else
20+
j_low = j_mid + 1;
21+
}
22+
// element not found
23+
printf("element not found\n");
24+
}
25+
26+
// Function to perform binary search on the mid values of row to get the desired pair of rows
27+
// where the element can be found
28+
void modifiedBinarySearch(int mat[n][m], int n, int m, int x)
29+
{ // If Single row matrix
30+
if (n == 1){
31+
binarySearch(mat, 0, 0, m-1, x);
32+
return;
33+
}
34+
35+
// Do binary search in middle column.
36+
// Condition to terminate the loop when the 2 desired rows are found.
37+
int i_low = 0, i_high = n-1, j_mid = m/2;
38+
while ((i_low+1) < i_high)
39+
{
40+
int i_mid = (i_low + i_high) / 2;
41+
// element found
42+
if (mat[i_mid][j_mid] == x){
43+
printf("Found at (%d,%d)\n",i_mid,j_mid);
44+
return;
45+
}
46+
else if (mat[i_mid][j_mid] > x)
47+
i_high = i_mid;
48+
else
49+
i_low = i_mid;
50+
}
51+
// If element is present on the mid of the two rows
52+
if (mat[i_low][j_mid] == x)
53+
printf("Found at (%d,%d)\n",i_low,j_mid);
54+
else if (mat[i_low+1][j_mid] == x)
55+
printf("Found at (%d,%d)\n",i_low+1,j_mid);
56+
57+
// Search element on 1st half of 1st row
58+
else if (x <= mat[i_low][j_mid-1])
59+
binarySearch(mat, i_low, 0, j_mid-1, x);
60+
61+
// Search element on 2nd half of 1st row
62+
else if (x >= mat[i_low][j_mid+1] && x <= mat[i_low][m-1])
63+
binarySearch(mat, i_low, j_mid+1, m-1, x);
64+
65+
// Search element on 1st half of 2nd row
66+
else if (x <= mat[i_low+1][j_mid-1])
67+
binarySearch(mat, i_low+1, 0, j_mid-1, x);
68+
69+
// search element on 2nd half of 2nd row
70+
else
71+
binarySearch(mat, i_low+1, j_mid+1, m-1, x);
72+
}
73+
74+
int main()
75+
{
76+
int x; //element to be searched
77+
scanf("%d %d %d\n",&n,&m,&x);
78+
int mat[n][m];
79+
for(int i=0; i<n; i++){
80+
for(int j=0; j<m; j++){
81+
scanf("%d",&mat[i][j]);
82+
}
83+
}
84+
modifiedBinarySearch(mat, n, m, x);
85+
return 0;
86+
}

misc/lexicographicPermutations.c

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
#include <stdio.h>
2+
#include <string.h>
3+
#include <stdlib.h>
4+
5+
void swap(char* left, char* right){
6+
char temp = *left;
7+
*left = *right;
8+
*right = temp;
9+
}
10+
11+
int compare (const void * a, const void * b){
12+
return ( *(char*)a - *(char*)b );
13+
}
14+
15+
void PrintSortedPermutations(char str[])
16+
{
17+
int strSize = strlen(str);
18+
qsort(str, strSize, sizeof(char), compare);
19+
20+
int largerPermFound = 1;
21+
do{
22+
// 1. Print permutation
23+
printf("%s\n", str);
24+
// 2. Find rightmost char that is smaller than char to its right
25+
int i;
26+
for (i = strSize - 2; i >= 0 && str[i] >= str[i+1]; --i){}
27+
28+
// if we couldn't find one, we're finished, else we can swap
29+
if (i >= 0){
30+
// 3. find character at index j such that str[j] = min(str[k]) && str[k] > str[i] for all k > i
31+
int j = i+1, k;
32+
for(k=j; k<strSize && str[k]; k++){
33+
if (str[k] > str[i] && str[k] < str[j])
34+
j = k;
35+
}
36+
// 3. Swap chars at i and j
37+
swap(&str[i], &str[j]);
38+
// 4. Sort string to the right of i
39+
qsort(str+i+1, strSize-i-1, sizeof(char), compare);
40+
}
41+
else largerPermFound = 0;
42+
}
43+
while(largerPermFound);
44+
}
45+
46+
int main() {
47+
int n; //size of string
48+
scanf("%d\n",&n);
49+
char str[n];
50+
scanf("%s",str);
51+
PrintSortedPermutations(str);
52+
return 0;
53+
}

0 commit comments

Comments
 (0)