Merge pull request #123 from agnimish/master

3 very useful codes added
This commit is contained in:
Christian Bender 2018-03-20 16:12:09 +01:00 committed by GitHub
commit 3dc2ecc520
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 242 additions and 0 deletions

View File

@ -0,0 +1,103 @@
#include<stdio.h>
#include<stdlib.h>
struct node{
int data;
struct node *next;
};
struct node *head1 = NULL;
struct node *head2 = NULL;
///// MAIN ALGORITHMIC FUNCTION to MERGE the two input linked lists ///////
void merge()
{
struct node *temp1 = head1;
struct node *temp2 = head2;
struct node *holder1 = NULL;
struct node *holder2 = NULL;
//Temporary pointer variables to store the address of next node of the two input linked list
while(temp1!=NULL && temp2!=NULL)
{
holder1 = temp1 -> next;
//Storing the address of next node of first linked list
temp1->next=temp2;
//Making the first node of first linked list point to first node of second linked list
if(holder1!=NULL) {
//Making the first node of second linked list point to second node of first linked list
holder2 = temp2 -> next;
temp2 -> next = holder1;
}
temp1=holder1;
temp2=holder2;
//Updating the address location of two pointer variables temp1 and temp2
}
}
void printlist(struct node *temp){
printf("%d",temp->data);
temp=temp->next;
while(temp!=NULL){
printf("->%d",temp->data);
temp=temp->next;
}
printf("\n");
}
int main()
{
// Linked List 1: 1->3->5->7 : Linked List 2: 2->4->6
// making lists
struct node *one = (struct node*)malloc(sizeof(struct node));
struct node *two = (struct node*)malloc(sizeof(struct node));
struct node *three = (struct node*)malloc(sizeof(struct node));
struct node *four = (struct node*)malloc(sizeof(struct node));
struct node *five = (struct node*)malloc(sizeof(struct node));
struct node *six = (struct node*)malloc(sizeof(struct node));
struct node *seven = (struct node*)malloc(sizeof(struct node));
//Seven nodes are created
head1=one;
head2=two;
//head1 points to first node of first linked list
//head2 points to first node of second linked list
one->data=1;
one->next=three;
two->data=2;
two->next=four;
three->data=3;
three->next=five;
four->data=4;
four->next=six;
five->data=5;
five->next=seven;
six->data=6;
six->next=NULL;
//Last node of second input linked list
seven->data=7;
seven->next=NULL;
//Last node of first input linked list
printf("Linked List 1: ");
printlist(head1);
printf("\nLinked List 2: ");
printlist(head2);
//Merging the two linked list into single linked list
merge();
printf("\nMerged Linked List: ");
printlist(head1); //list one has been modified
return 0;
}

View File

@ -0,0 +1,86 @@
#include<stdio.h>
int n,m; //size of the matrix
// This function does Binary search for x in i-th row from j_low to j_high.
void binarySearch(int mat[n][m], int i, int j_low,int j_high, int x)
{
while (j_low <= j_high)
{
int j_mid = (j_low + j_high) / 2;
// Element found
if (mat[i][j_mid] == x){
printf("Found at (%d,%d)\n",i,j_mid);
return ;
}
else if (mat[i][j_mid] > x)
j_high = j_mid - 1;
else
j_low = j_mid + 1;
}
// element not found
printf("element not found\n");
}
// Function to perform binary search on the mid values of row to get the desired pair of rows
// where the element can be found
void modifiedBinarySearch(int mat[n][m], int n, int m, int x)
{ // If Single row matrix
if (n == 1){
binarySearch(mat, 0, 0, m-1, x);
return;
}
// Do binary search in middle column.
// Condition to terminate the loop when the 2 desired rows are found.
int i_low = 0, i_high = n-1, j_mid = m/2;
while ((i_low+1) < i_high)
{
int i_mid = (i_low + i_high) / 2;
// element found
if (mat[i_mid][j_mid] == x){
printf("Found at (%d,%d)\n",i_mid,j_mid);
return;
}
else if (mat[i_mid][j_mid] > x)
i_high = i_mid;
else
i_low = i_mid;
}
// If element is present on the mid of the two rows
if (mat[i_low][j_mid] == x)
printf("Found at (%d,%d)\n",i_low,j_mid);
else if (mat[i_low+1][j_mid] == x)
printf("Found at (%d,%d)\n",i_low+1,j_mid);
// Search element on 1st half of 1st row
else if (x <= mat[i_low][j_mid-1])
binarySearch(mat, i_low, 0, j_mid-1, x);
// Search element on 2nd half of 1st row
else if (x >= mat[i_low][j_mid+1] && x <= mat[i_low][m-1])
binarySearch(mat, i_low, j_mid+1, m-1, x);
// Search element on 1st half of 2nd row
else if (x <= mat[i_low+1][j_mid-1])
binarySearch(mat, i_low+1, 0, j_mid-1, x);
// search element on 2nd half of 2nd row
else
binarySearch(mat, i_low+1, j_mid+1, m-1, x);
}
int main()
{
int x; //element to be searched
scanf("%d %d %d\n",&n,&m,&x);
int mat[n][m];
for(int i=0; i<n; i++){
for(int j=0; j<m; j++){
scanf("%d",&mat[i][j]);
}
}
modifiedBinarySearch(mat, n, m, x);
return 0;
}

View File

@ -0,0 +1,53 @@
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
void swap(char* left, char* right){
char temp = *left;
*left = *right;
*right = temp;
}
int compare (const void * a, const void * b){
return ( *(char*)a - *(char*)b );
}
void PrintSortedPermutations(char str[])
{
int strSize = strlen(str);
qsort(str, strSize, sizeof(char), compare);
int largerPermFound = 1;
do{
// 1. Print permutation
printf("%s\n", str);
// 2. Find rightmost char that is smaller than char to its right
int i;
for (i = strSize - 2; i >= 0 && str[i] >= str[i+1]; --i){}
// if we couldn't find one, we're finished, else we can swap
if (i >= 0){
// 3. find character at index j such that str[j] = min(str[k]) && str[k] > str[i] for all k > i
int j = i+1, k;
for(k=j; k<strSize && str[k]; k++){
if (str[k] > str[i] && str[k] < str[j])
j = k;
}
// 3. Swap chars at i and j
swap(&str[i], &str[j]);
// 4. Sort string to the right of i
qsort(str+i+1, strSize-i-1, sizeof(char), compare);
}
else largerPermFound = 0;
}
while(largerPermFound);
}
int main() {
int n; //size of string
scanf("%d\n",&n);
char str[n];
scanf("%s",str);
PrintSortedPermutations(str);
return 0;
}