mirror of
https://github.moeyy.xyz/https://github.com/TheAlgorithms/C.git
synced 2023-10-11 15:56:24 +08:00
Merge branch 'master' of https://github.com/kvedala/C
This commit is contained in:
commit
aa8d74543f
@ -7,7 +7,6 @@
|
||||
|
||||
int main()
|
||||
{
|
||||
|
||||
int remainder, number = 0, decimal_number = 0, temp = 1;
|
||||
printf("/n Enter any binary number= ");
|
||||
scanf("%d", &number);
|
||||
@ -15,7 +14,6 @@ int main()
|
||||
// Iterate over the number until the end.
|
||||
while (number > 0)
|
||||
{
|
||||
|
||||
remainder = number % 10;
|
||||
number = number / 10;
|
||||
decimal_number += remainder * temp;
|
||||
|
@ -5,7 +5,6 @@
|
||||
|
||||
int main()
|
||||
{
|
||||
|
||||
// input of the user
|
||||
int inputNumber;
|
||||
|
||||
@ -35,7 +34,6 @@ int main()
|
||||
// actual processing
|
||||
while (inputNumber > 0)
|
||||
{
|
||||
|
||||
// computes the remainder by modulo 2
|
||||
re = inputNumber % 2;
|
||||
|
||||
|
@ -4,7 +4,6 @@ void decimal2Hexadecimal(long num);
|
||||
|
||||
int main()
|
||||
{
|
||||
|
||||
long decimalnum;
|
||||
|
||||
printf("Enter decimal number: ");
|
||||
@ -19,7 +18,6 @@ int main()
|
||||
* number****************/
|
||||
void decimal2Hexadecimal(long num)
|
||||
{
|
||||
|
||||
long decimalnum = num;
|
||||
long quotient, remainder;
|
||||
int i, j = 0;
|
||||
@ -29,7 +27,6 @@ void decimal2Hexadecimal(long num)
|
||||
|
||||
while (quotient != 0)
|
||||
{
|
||||
|
||||
remainder = quotient % 16;
|
||||
if (remainder < 10)
|
||||
hexadecimalnum[j++] = 48 + remainder;
|
||||
|
@ -4,7 +4,6 @@ void decimal2Octal(long decimalnum);
|
||||
|
||||
int main()
|
||||
{
|
||||
|
||||
long decimalnum;
|
||||
|
||||
printf("Enter the decimal number: ");
|
||||
@ -30,9 +29,7 @@ void decimal2Octal(long decimalnum)
|
||||
quotient = quotient / 8;
|
||||
}
|
||||
|
||||
for (j = i - 1; j > 0; j--)
|
||||
|
||||
printf("%d", octalNumber[j]);
|
||||
for (j = i - 1; j > 0; j--) printf("%d", octalNumber[j]);
|
||||
|
||||
printf("\n");
|
||||
}
|
||||
|
@ -6,12 +6,10 @@ int convertValue(int num, int i) { return num * pow(8, i); }
|
||||
|
||||
long long toDecimal(int octal_value)
|
||||
{
|
||||
|
||||
int decimal_value = 0, i = 0;
|
||||
|
||||
while (octal_value)
|
||||
{
|
||||
|
||||
// Extracts right-most digit and then multiplies by 8^i
|
||||
decimal_value += convertValue(octal_value % 10, i++);
|
||||
|
||||
@ -24,7 +22,6 @@ long long toDecimal(int octal_value)
|
||||
|
||||
int main()
|
||||
{
|
||||
|
||||
printf("Enter octal value: ");
|
||||
|
||||
int octal_value;
|
||||
|
@ -13,10 +13,10 @@
|
||||
*
|
||||
*/
|
||||
|
||||
#include "CArray.h"
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <time.h>
|
||||
#include "CArray.h"
|
||||
|
||||
int CArrayTests()
|
||||
{
|
||||
|
@ -50,8 +50,7 @@ avlNode *minNode(avlNode *node)
|
||||
{
|
||||
avlNode *temp = node;
|
||||
|
||||
while (temp->left != NULL)
|
||||
temp = temp->left;
|
||||
while (temp->left != NULL) temp = temp->left;
|
||||
|
||||
return temp;
|
||||
}
|
||||
@ -64,8 +63,7 @@ void printAVL(avlNode *node, int level)
|
||||
printAVL(node->right, level + 1);
|
||||
printf("\n\n");
|
||||
|
||||
for (i = 0; i < level; i++)
|
||||
printf("\t");
|
||||
for (i = 0; i < level; i++) printf("\t");
|
||||
|
||||
printf("%d", node->key);
|
||||
|
||||
@ -117,7 +115,6 @@ avlNode *RightLeftRotate(avlNode *z)
|
||||
|
||||
avlNode *insert(avlNode *node, int key)
|
||||
{
|
||||
|
||||
if (node == NULL)
|
||||
return (newNode(key));
|
||||
|
||||
@ -310,7 +307,6 @@ int main()
|
||||
|
||||
case 1:
|
||||
{
|
||||
|
||||
printf("\n\tEnter the Number to insert: ");
|
||||
scanf("%d", &insertNum);
|
||||
|
||||
|
@ -12,7 +12,6 @@
|
||||
// Node, the basic data structure in the tree
|
||||
typedef struct node
|
||||
{
|
||||
|
||||
// left child
|
||||
struct node *left;
|
||||
|
||||
@ -27,7 +26,6 @@ typedef struct node
|
||||
// pointer
|
||||
node *newNode(int data)
|
||||
{
|
||||
|
||||
// creates a slug
|
||||
node *tmp = (node *)malloc(sizeof(node));
|
||||
|
||||
@ -110,7 +108,6 @@ node *delete (node *root, int data)
|
||||
// subtree and switch with the root's
|
||||
else
|
||||
{
|
||||
|
||||
// finds the biggest node in the left branch.
|
||||
node *tmp = getMax(root->left);
|
||||
|
||||
@ -190,7 +187,6 @@ void inOrder(node *root)
|
||||
|
||||
void main()
|
||||
{
|
||||
|
||||
// this reference don't change.
|
||||
// only the tree changes.
|
||||
node *root = NULL;
|
||||
@ -200,7 +196,8 @@ void main()
|
||||
// event-loop.
|
||||
while (opt != 0)
|
||||
{
|
||||
printf("\n\n[1] Insert Node\n[2] Delete Node\n[3] Find a Node\n[4] Get "
|
||||
printf(
|
||||
"\n\n[1] Insert Node\n[2] Delete Node\n[3] Find a Node\n[4] Get "
|
||||
"current Height\n[5] Print Tree in Crescent Order\n[0] Quit\n");
|
||||
scanf("%d", &opt); // reads the choice of the user
|
||||
|
||||
|
@ -91,7 +91,6 @@ Node *rightRotate(Node *node)
|
||||
// Check the node after the insertion step
|
||||
void checkNode(Node *node)
|
||||
{
|
||||
|
||||
// If the node is the root
|
||||
if (node == NULL || node->par == NULL)
|
||||
{
|
||||
@ -307,7 +306,6 @@ void insertNode(int val, Node **root)
|
||||
}
|
||||
else
|
||||
{
|
||||
|
||||
// Go right
|
||||
if (buffRoot->right != NULL)
|
||||
{
|
||||
@ -344,7 +342,6 @@ void insertNode(int val, Node **root)
|
||||
|
||||
void checkForCase2(Node *toDelete, int delete, int fromDirection, Node **root)
|
||||
{
|
||||
|
||||
if (toDelete == (*root))
|
||||
{
|
||||
(*root)->color = 0;
|
||||
@ -391,11 +388,9 @@ void checkForCase2(Node *toDelete, int delete, int fromDirection, Node **root)
|
||||
{
|
||||
if (sibling->right != NULL && sibling->right->color == 1)
|
||||
{
|
||||
|
||||
// Sibling is left and child is right. i.e. LEFT RIGHT ROTATION
|
||||
if (locateChild == 1)
|
||||
{
|
||||
|
||||
int parColor = parent->color;
|
||||
|
||||
// Step 1: Left rotate sibling
|
||||
@ -460,11 +455,9 @@ void checkForCase2(Node *toDelete, int delete, int fromDirection, Node **root)
|
||||
}
|
||||
else
|
||||
{
|
||||
|
||||
// Sibling is right and child is left. i.e. RIGHT LEFT ROTATION
|
||||
if (locateChild == 0)
|
||||
{
|
||||
|
||||
int parColor = parent->color;
|
||||
|
||||
// Step 1: Right rotate sibling
|
||||
@ -616,7 +609,6 @@ void deleteNode(int val, Node **root)
|
||||
// Search for the element in the tree
|
||||
while (1)
|
||||
{
|
||||
|
||||
if (val == buffRoot->val)
|
||||
{
|
||||
// Node Found
|
||||
@ -684,7 +676,6 @@ void deleteNode(int val, Node **root)
|
||||
(toDelete->left != NULL && toDelete->left->color == 1) ||
|
||||
(toDelete->right != NULL && toDelete->right->color == 1))
|
||||
{
|
||||
|
||||
// if it is a leaf
|
||||
if (toDelete->left == NULL && toDelete->right == NULL)
|
||||
{
|
||||
@ -755,7 +746,8 @@ int main()
|
||||
{
|
||||
Node *root = NULL;
|
||||
int scanValue, choice = 1;
|
||||
printf("1 - Input\n2 - Delete\n3 - Inorder Traversel\n0 - Quit\n\nPlease "
|
||||
printf(
|
||||
"1 - Input\n2 - Delete\n3 - Inorder Traversel\n0 - Quit\n\nPlease "
|
||||
"Enter the Choice - ");
|
||||
scanf("%d", &choice);
|
||||
while (choice)
|
||||
@ -795,7 +787,8 @@ int main()
|
||||
printf("Root - %d\n", root->val);
|
||||
}
|
||||
}
|
||||
printf("1 - Input\n2 - Delete\n3 - Inorder Traversel\n0 - "
|
||||
printf(
|
||||
"1 - Input\n2 - Delete\n3 - Inorder Traversel\n0 - "
|
||||
"Quit\n\nPlease Enter the Choice - ");
|
||||
scanf("%d", &choice);
|
||||
}
|
||||
|
@ -1,6 +1,6 @@
|
||||
#include "dynamic_array.h"
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include "dynamic_array.h"
|
||||
|
||||
int main()
|
||||
{
|
||||
|
@ -74,8 +74,7 @@ void BellmanFord(struct Graph *graph, int src)
|
||||
|
||||
// Initialize distances array as INF for all except source
|
||||
// Intialize source as zero
|
||||
for (int i = 0; i < V; i++)
|
||||
dist[i] = INT_MAX;
|
||||
for (int i = 0; i < V; i++) dist[i] = INT_MAX;
|
||||
dist[src] = 0;
|
||||
|
||||
// Calculate shortest path distance from source to all edges
|
||||
@ -100,7 +99,8 @@ void BellmanFord(struct Graph *graph, int src)
|
||||
|
||||
if (dist[u] != INT_MAX && dist[u] + w < dist[v])
|
||||
{
|
||||
printf("Graph contains negative weight cycle. Hence, shortest "
|
||||
printf(
|
||||
"Graph contains negative weight cycle. Hence, shortest "
|
||||
"distance not guaranteed.");
|
||||
return;
|
||||
}
|
||||
|
@ -1,7 +1,7 @@
|
||||
#include "Graph.h"
|
||||
#include "queue.h"
|
||||
#include <stdbool.h>
|
||||
#include <stdio.h>
|
||||
#include "Graph.h"
|
||||
#include "queue.h"
|
||||
|
||||
#define MAX_NODES 1000
|
||||
|
||||
@ -11,8 +11,7 @@ int visited[MAX_NODES]; // array to store visiting order
|
||||
bool findPathBFS(Graph g, int nV, Vertex src, Vertex dest)
|
||||
{
|
||||
Vertex v;
|
||||
for (v = 0; v < nV; v++)
|
||||
visited[v] = -1;
|
||||
for (v = 0; v < nV; v++) visited[v] = -1;
|
||||
|
||||
visited[src] = src;
|
||||
queue Q = newQueue();
|
||||
|
@ -1,6 +1,6 @@
|
||||
#include "Graph.h"
|
||||
#include <stdbool.h>
|
||||
#include <stdio.h>
|
||||
#include "Graph.h"
|
||||
|
||||
#define MAX_NODES 1000
|
||||
|
||||
@ -25,8 +25,7 @@ bool dfsPathCheck(Graph g, int nV, Vertex v, Vertex dest)
|
||||
bool findPathDFS(Graph g, int nV, Vertex src, Vertex dest)
|
||||
{
|
||||
Vertex v;
|
||||
for (v = 0; v < nV; v++)
|
||||
visited[v] = -1;
|
||||
for (v = 0; v < nV; v++) visited[v] = -1;
|
||||
visited[src] = src;
|
||||
return dfsPathCheck(g, nV, src, dest);
|
||||
}
|
||||
|
@ -18,8 +18,7 @@ void createGraph(struct Graph *G, int V)
|
||||
for (int i = 0; i < V; i++)
|
||||
{
|
||||
G->edges[i] = (int *)malloc(V * sizeof(int));
|
||||
for (int j = 0; j < V; j++)
|
||||
G->edges[i][j] = INT_MAX;
|
||||
for (int j = 0; j < V; j++) G->edges[i][j] = INT_MAX;
|
||||
G->edges[i][i] = 0;
|
||||
}
|
||||
}
|
||||
@ -68,8 +67,7 @@ void Dijkstra(struct Graph *graph, int src)
|
||||
// in the shortest path tree
|
||||
|
||||
// Initialise mdist and vset. Set distance of source as zero
|
||||
for (int i = 0; i < V; i++)
|
||||
mdist[i] = INT_MAX, vset[i] = 0;
|
||||
for (int i = 0; i < V; i++) mdist[i] = INT_MAX, vset[i] = 0;
|
||||
|
||||
mdist[src] = 0;
|
||||
|
||||
|
@ -1,6 +1,6 @@
|
||||
#include "Graph.h"
|
||||
#include <stdbool.h>
|
||||
#include <stdio.h>
|
||||
#include "Graph.h"
|
||||
|
||||
// Return the number of vertices that v is
|
||||
// connected to
|
||||
|
@ -18,8 +18,7 @@ void createGraph(struct Graph *G, int V)
|
||||
for (int i = 0; i < V; i++)
|
||||
{
|
||||
G->edges[i] = (int *)malloc(V * sizeof(int));
|
||||
for (int j = 0; j < V; j++)
|
||||
G->edges[i][j] = INT_MAX;
|
||||
for (int j = 0; j < V; j++) G->edges[i][j] = INT_MAX;
|
||||
G->edges[i][i] = 0;
|
||||
}
|
||||
}
|
||||
@ -38,7 +37,6 @@ void print(int dist[], int V)
|
||||
{
|
||||
for (int j = 0; j < V; j++)
|
||||
{
|
||||
|
||||
if (dist[i * V + j] != INT_MAX)
|
||||
printf("%d\t", dist[i * V + j]);
|
||||
else
|
||||
@ -57,8 +55,7 @@ void FloydWarshall(struct Graph *graph)
|
||||
|
||||
// Initialise distance array
|
||||
for (int i = 0; i < V; i++)
|
||||
for (int j = 0; j < V; j++)
|
||||
dist[i][j] = graph->edges[i][j];
|
||||
for (int j = 0; j < V; j++) dist[i][j] = graph->edges[i][j];
|
||||
|
||||
// Calculate distances
|
||||
for (int k = 0; k < V; k++)
|
||||
@ -79,8 +76,7 @@ void FloydWarshall(struct Graph *graph)
|
||||
// Convert 2d array to 1d array for print
|
||||
int dist1d[V * V];
|
||||
for (int i = 0; i < V; i++)
|
||||
for (int j = 0; j < V; j++)
|
||||
dist1d[i * V + j] = dist[i][j];
|
||||
for (int j = 0; j < V; j++) dist1d[i * V + j] = dist[i][j];
|
||||
|
||||
print(dist1d, V);
|
||||
}
|
||||
|
@ -87,8 +87,7 @@ void freeGraph(Graph g)
|
||||
assert(g != NULL);
|
||||
|
||||
int i;
|
||||
for (i = 0; i < g->nV; i++)
|
||||
free(g->edges[i]);
|
||||
for (i = 0; i < g->nV; i++) free(g->edges[i]);
|
||||
free(g->edges);
|
||||
free(g);
|
||||
}
|
||||
|
@ -1,6 +1,6 @@
|
||||
#include "Graph.h"
|
||||
#include <stdbool.h>
|
||||
#include <stdio.h>
|
||||
#include "Graph.h"
|
||||
|
||||
#define MAX_NODES 1000
|
||||
|
||||
@ -38,8 +38,7 @@ bool hamiltonR(Graph g, int nV, Vertex v, Vertex dest, int d)
|
||||
bool hasHamiltonianPath(Graph g, int nV, Vertex src, Vertex dest)
|
||||
{
|
||||
Vertex v;
|
||||
for (v = 0; v < nV; v++)
|
||||
visited[v] = false;
|
||||
for (v = 0; v < nV; v++) visited[v] = false;
|
||||
return hamiltonR(g, nV, src, dest, nV - 1);
|
||||
}
|
||||
|
||||
|
@ -97,8 +97,7 @@ void topologicalSort(struct Graph *graph)
|
||||
topologicalSortHelper(i, graph, stack);
|
||||
}
|
||||
}
|
||||
while (stack->top != -1)
|
||||
printf("%d ", pop(stack));
|
||||
while (stack->top != -1) printf("%d ", pop(stack));
|
||||
}
|
||||
// Allocate memory for a node
|
||||
struct node *createNode(int v)
|
||||
|
@ -11,8 +11,7 @@ void warshall()
|
||||
{
|
||||
int i, s, t;
|
||||
for (s = 0; s < NODES; s++)
|
||||
for (t = 0; t < NODES; t++)
|
||||
tc[s][t] = digraph[s][t];
|
||||
for (t = 0; t < NODES; t++) tc[s][t] = digraph[s][t];
|
||||
|
||||
for (i = 0; i < NODES; i++)
|
||||
for (s = 0; s < NODES; s++)
|
||||
|
@ -69,7 +69,6 @@ void pop(struct node *p)
|
||||
|
||||
void display(struct node *p)
|
||||
{
|
||||
|
||||
if (top == NULL)
|
||||
printf("stack is empty\n");
|
||||
else
|
||||
|
@ -29,8 +29,7 @@ L List_push(L list, void *val)
|
||||
int List_length(L list)
|
||||
{
|
||||
int n;
|
||||
for (n = 0; list; list = list->next)
|
||||
n++;
|
||||
for (n = 0; list; list = list->next) n++;
|
||||
return n;
|
||||
}
|
||||
|
||||
|
@ -1,14 +1,13 @@
|
||||
#include "list.h"
|
||||
#include <assert.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include "list.h"
|
||||
|
||||
void print_list(char **array)
|
||||
{
|
||||
int i;
|
||||
for (i = 0; array[i]; i++)
|
||||
printf("%s", array[i]);
|
||||
for (i = 0; array[i]; i++) printf("%s", array[i]);
|
||||
printf("\n");
|
||||
}
|
||||
|
||||
|
@ -33,7 +33,6 @@ int isEmpty();
|
||||
|
||||
int main(int argc, char const *argv[])
|
||||
{
|
||||
|
||||
create();
|
||||
enque(5);
|
||||
|
||||
|
@ -38,7 +38,6 @@ int isEmpty();
|
||||
|
||||
int main(int argc, char const *argv[])
|
||||
{
|
||||
|
||||
int x, y, z;
|
||||
|
||||
create();
|
||||
|
@ -11,7 +11,6 @@ int a[100], top = -1;
|
||||
|
||||
int main()
|
||||
{
|
||||
|
||||
int x;
|
||||
while (1)
|
||||
{
|
||||
|
@ -33,7 +33,6 @@ int offset = -1;
|
||||
|
||||
void initStack()
|
||||
{
|
||||
|
||||
array = malloc(sizeof(void *) * max);
|
||||
assert(array); /* tests whether pointer is assigned to memory. */
|
||||
}
|
||||
@ -62,12 +61,10 @@ void grow()
|
||||
/* push: pushs the argument onto the stack */
|
||||
void push(void *object)
|
||||
{
|
||||
|
||||
assert(object); /* tests whether pointer isn't null */
|
||||
|
||||
if (counter < max)
|
||||
{
|
||||
|
||||
offset++; /* increases the element-pointer */
|
||||
|
||||
/*
|
||||
@ -81,7 +78,6 @@ void push(void *object)
|
||||
}
|
||||
else /* stack is full */
|
||||
{
|
||||
|
||||
grow(); /* lets grow stack */
|
||||
push(object); /* recursive call */
|
||||
}
|
||||
@ -92,7 +88,6 @@ void push(void *object)
|
||||
*/
|
||||
void *pop()
|
||||
{
|
||||
|
||||
void *top = *(array + offset);
|
||||
|
||||
/* check pointers */
|
||||
|
@ -1,7 +1,7 @@
|
||||
#include "stack.h"
|
||||
#include <assert.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include "stack.h"
|
||||
|
||||
int main()
|
||||
{
|
||||
|
@ -84,8 +84,7 @@ char *abbreviate(const char *phrase)
|
||||
strcat(acr, words[i]);
|
||||
}
|
||||
|
||||
for (i = 0; i < counter; i++)
|
||||
free(words[i]);
|
||||
for (i = 0; i < counter; i++) free(words[i]);
|
||||
free(words);
|
||||
|
||||
return acr;
|
||||
|
@ -6,7 +6,6 @@
|
||||
*/
|
||||
bool is_isogram(const char phrase[])
|
||||
{
|
||||
|
||||
/* use 'unsigned' because of the function strlen(...) */
|
||||
unsigned int i = 0;
|
||||
unsigned int j = 0;
|
||||
|
@ -4,7 +4,6 @@
|
||||
|
||||
char *to_rna(const char s[])
|
||||
{
|
||||
|
||||
/* determines the length of the given string */
|
||||
int len = strlen(s);
|
||||
|
||||
|
@ -41,7 +41,6 @@ int word_count(const char *input_text, word_count_word_t *words)
|
||||
input[index] = '\0';
|
||||
if (strlen(p_str) <= MAX_WORD_LENGTH)
|
||||
{
|
||||
|
||||
if (index_list <= MAX_WORDS)
|
||||
{
|
||||
strcpy(word_list[index_list], p_str);
|
||||
|
@ -63,7 +63,6 @@ void dijkstra(int s)
|
||||
|
||||
int main(int argc, char const *argv[])
|
||||
{
|
||||
|
||||
printf("Enter the number of vertices: ");
|
||||
scanf(" %d", &V);
|
||||
printf("Enter the adj matrix: ");
|
||||
|
@ -3,8 +3,8 @@
|
||||
This file contains a simple test program for each hash-function.
|
||||
*/
|
||||
|
||||
#include "hash.h"
|
||||
#include <stdio.h>
|
||||
#include "hash.h"
|
||||
|
||||
int main(void)
|
||||
{
|
||||
|
@ -4,7 +4,6 @@ int min(int a, int b) { return ((a < b) ? a : b); }
|
||||
// Two pointer approach to find maximum container area
|
||||
int maxArea(int *height, int heightSize)
|
||||
{
|
||||
|
||||
// Start with maximum container width
|
||||
int start = 0;
|
||||
int end = heightSize - 1;
|
||||
|
@ -1,7 +1,6 @@
|
||||
int distanceBetweenBusStops(int *distance, int distanceSize, int start,
|
||||
int destination)
|
||||
{
|
||||
|
||||
int sum1 = 0, sum2 = 0;
|
||||
if (start > destination)
|
||||
{
|
||||
|
@ -1,7 +1,6 @@
|
||||
int singleNumber(int *nums, int numsSize)
|
||||
{
|
||||
int i, result = 0;
|
||||
for (i = 0; i < numsSize; i++)
|
||||
result = result ^ nums[i];
|
||||
for (i = 0; i < numsSize; i++) result = result ^ nums[i];
|
||||
return result;
|
||||
}
|
||||
|
@ -7,19 +7,20 @@ uint32_t reverseBits(uint32_t n)
|
||||
{
|
||||
if ((n & (UINT32_C(1)
|
||||
<< i))) // if the bit on the ith position of 32 bit input is
|
||||
// 1, then proceed Further note the use of UINT32_C to
|
||||
// convert 1 to unsigned 32 bit int, since just 1 is
|
||||
// treated as int which cannot be shifted left more
|
||||
// than 30 times
|
||||
// 1, then proceed Further note the use of UINT32_C
|
||||
// to convert 1 to unsigned 32 bit int, since just 1
|
||||
// is treated as int which cannot be shifted left
|
||||
// more than 30 times
|
||||
reverse_int =
|
||||
reverse_int |
|
||||
(UINT32_C(1)
|
||||
<< (TotalBits - 1 -
|
||||
i)); // Convert the ith bit from the end in reverse_int
|
||||
// from 0 to 1, if ith bit from beginning in n is 1
|
||||
// This is achieved by using bitwise OR on reverse_int
|
||||
// (where ith bit from end is currently 0) and 1
|
||||
// shifted left 31 - i bits (to ith bit from the end)
|
||||
// This is achieved by using bitwise OR on
|
||||
// reverse_int (where ith bit from end is currently
|
||||
// 0) and 1 shifted left 31 - i bits (to ith bit from
|
||||
// the end)
|
||||
}
|
||||
return reverse_int;
|
||||
}
|
@ -2,7 +2,6 @@ bool isPowerOfTwo(int n)
|
||||
{
|
||||
if (!n)
|
||||
return false;
|
||||
while (n % 2 == 0)
|
||||
n /= 2;
|
||||
while (n % 2 == 0) n /= 2;
|
||||
return n == 1;
|
||||
}
|
@ -4,14 +4,11 @@ bool isAnagram(char *s, char *t)
|
||||
int m = strlen(t);
|
||||
|
||||
int cnt_s[1000], cnt_t[1000];
|
||||
for (int c = 97; c < 97 + 26; c++)
|
||||
cnt_s[c] = cnt_t[c] = 0;
|
||||
for (int c = 97; c < 97 + 26; c++) cnt_s[c] = cnt_t[c] = 0;
|
||||
|
||||
for (int i = 0; i < n; i++)
|
||||
cnt_s[s[i]]++;
|
||||
for (int i = 0; i < n; i++) cnt_s[s[i]]++;
|
||||
|
||||
for (int i = 0; i < m; i++)
|
||||
cnt_t[t[i]]++;
|
||||
for (int i = 0; i < m; i++) cnt_t[t[i]]++;
|
||||
|
||||
for (int c = 97; c < 97 + 26; c++)
|
||||
if (cnt_s[c] != cnt_t[c])
|
||||
|
@ -1,6 +1,5 @@
|
||||
int lengthOfLongestSubstring(char *str)
|
||||
{
|
||||
|
||||
int n = strlen(str);
|
||||
|
||||
if (!n)
|
||||
@ -57,8 +56,7 @@ int lengthOfLongestSubstring(char *s)
|
||||
if (cur_max >= max)
|
||||
max = cur_max;
|
||||
cur_max = 0;
|
||||
while (s[end - 1] != c)
|
||||
end--;
|
||||
while (s[end - 1] != c) end--;
|
||||
}
|
||||
}
|
||||
if (cur_max >= max)
|
||||
|
@ -1,6 +1,5 @@
|
||||
char *countAndSay(int n)
|
||||
{
|
||||
|
||||
// Calculating the length of array
|
||||
double result = 1.0;
|
||||
for (int i = 0; i < n - 1; i++)
|
||||
|
@ -2,8 +2,7 @@ int firstUniqChar(char *s)
|
||||
{
|
||||
int *arr = calloc(256, sizeof(int));
|
||||
int i;
|
||||
for (i = 0; i < strlen(s); i++)
|
||||
arr[s[i]] = arr[s[i]] + 1;
|
||||
for (i = 0; i < strlen(s); i++) arr[s[i]] = arr[s[i]] + 1;
|
||||
for (i = 0; i < strlen(s); i++)
|
||||
{
|
||||
if (arr[s[i]] == 1)
|
||||
|
@ -2,9 +2,7 @@ char findTheDifference(char *s, char *t)
|
||||
{
|
||||
int sum1 = 0, sum2 = 0;
|
||||
int i;
|
||||
for (i = 0; i < strlen(s); i++)
|
||||
sum1 += s[i];
|
||||
for (i = 0; i < strlen(t); i++)
|
||||
sum2 += t[i];
|
||||
for (i = 0; i < strlen(s); i++) sum1 += s[i];
|
||||
for (i = 0; i < strlen(t); i++) sum2 += t[i];
|
||||
return (char)(sum2 - sum1);
|
||||
}
|
||||
|
@ -2,7 +2,6 @@ int cmpval(const void *a, const void *b) { return *(int *)a - *(int *)b; }
|
||||
|
||||
int *findDuplicates(int *nums, int numsSize, int *returnSize)
|
||||
{
|
||||
|
||||
int i;
|
||||
qsort(nums, numsSize, sizeof(int), cmpval);
|
||||
int *retArr = malloc(numsSize * sizeof(int));
|
||||
|
@ -2,18 +2,18 @@ int hammingDistance(int x, int y)
|
||||
{
|
||||
int difference =
|
||||
x ^ y; // The XOR operator generates the bitwise difference in the
|
||||
// binary representation of two numbers If bit in ith position of
|
||||
// both numbers is same, bit in difference is 0, otherwise 1
|
||||
// binary representation of two numbers If bit in ith position
|
||||
// of both numbers is same, bit in difference is 0, otherwise 1
|
||||
int TotalBits = sizeof(difference) * 8; // total number of bits
|
||||
int i, distance = 0;
|
||||
for (i = 0; i < TotalBits; i++)
|
||||
{
|
||||
if (difference &
|
||||
(UINT32_C(1)
|
||||
<< i)) // if the bit on the ith position of 32 bit input is 1, then
|
||||
// proceed Further note the use of UINT32_C to convert 1 to
|
||||
// unsigned 32 bit int, as just 1 is treated as int which
|
||||
// cannot be shifted left more than 30 times
|
||||
<< i)) // if the bit on the ith position of 32 bit input is 1,
|
||||
// then proceed Further note the use of UINT32_C to convert
|
||||
// 1 to unsigned 32 bit int, as just 1 is treated as int
|
||||
// which cannot be shifted left more than 30 times
|
||||
distance += 1;
|
||||
}
|
||||
return distance;
|
||||
|
@ -10,9 +10,9 @@ int findComplement(int num)
|
||||
// bit to underflow every iteration till it becomes 0
|
||||
}
|
||||
int i,
|
||||
flipNumber =
|
||||
1; // Eg: 1's complement of 101(binary) can be found as 101^111 (XOR
|
||||
// with 111 flips all bits that are 1 to 0 and flips 0 to 1)
|
||||
flipNumber = 1; // Eg: 1's complement of 101(binary) can be found as
|
||||
// 101^111 (XOR with 111 flips all bits that are 1 to 0
|
||||
// and flips 0 to 1)
|
||||
for (i = 1; i < TotalBits; i++)
|
||||
{
|
||||
flipNumber += UINT32_C(1)
|
||||
|
@ -3,7 +3,6 @@ int arrayPairSum(int *nums, int numsSize)
|
||||
{
|
||||
int sum = 0, i;
|
||||
qsort(nums, numsSize, sizeof(int), cmpval);
|
||||
for (i = 0; i < numsSize; i = i + 2)
|
||||
sum = sum + nums[i];
|
||||
for (i = 0; i < numsSize; i = i + 2) sum = sum + nums[i];
|
||||
return sum;
|
||||
}
|
||||
|
@ -1,6 +1,5 @@
|
||||
char *toLowerCase(char *str)
|
||||
{
|
||||
for (int i = 0; i < strlen(str); i++)
|
||||
str[i] = tolower(str[i]);
|
||||
for (int i = 0; i < strlen(str); i++) str[i] = tolower(str[i]);
|
||||
return str;
|
||||
}
|
||||
|
@ -8,12 +8,10 @@ int numJewelsInStones(char *j, char *s)
|
||||
memset(cnt, 0, sizeof(cnt));
|
||||
|
||||
// lookup to know which character occurs in j
|
||||
for (int i = 0; i < lenj; i++)
|
||||
cnt[j[i]]++;
|
||||
for (int i = 0; i < lenj; i++) cnt[j[i]]++;
|
||||
|
||||
// count the characters in s
|
||||
for (int i = 0; i < lens; i++)
|
||||
sol += cnt[s[i]];
|
||||
for (int i = 0; i < lens; i++) sol += cnt[s[i]];
|
||||
|
||||
return sol;
|
||||
}
|
||||
|
@ -26,8 +26,7 @@ int cmpval(const void *a, const void *b) { return *(int *)a - *(int *)b; }
|
||||
int *sortedSquares(int *A, int ASize, int *returnSize)
|
||||
{
|
||||
int *res = malloc(ASize * sizeof(int));
|
||||
for (int i = 0; i < ASize; i++)
|
||||
res[i] = A[i] * A[i];
|
||||
for (int i = 0; i < ASize; i++) res[i] = A[i] * A[i];
|
||||
*returnSize = ASize;
|
||||
qsort(res, ASize, sizeof(int), cmpval);
|
||||
return res;
|
||||
|
@ -35,7 +35,6 @@
|
||||
/** structure to hold adaline model parameters */
|
||||
struct adaline
|
||||
{
|
||||
|
||||
double eta; ///< learning rate of the algorithm
|
||||
double *weights; ///< weights of the neural network
|
||||
int num_weights; ///< number of weights of the neural network
|
||||
@ -70,8 +69,7 @@ struct adaline new_adaline(const int num_features, const double eta)
|
||||
}
|
||||
|
||||
// initialize with random weights in the range [-50, 49]
|
||||
for (int i = 0; i < num_weights; i++)
|
||||
ada.weights[i] = 1.f;
|
||||
for (int i = 0; i < num_weights; i++) ada.weights[i] = 1.f;
|
||||
// ada.weights[i] = (double)(rand() % 100) - 50);
|
||||
|
||||
return ada;
|
||||
@ -126,8 +124,7 @@ int predict(struct adaline *ada, const double *x, double *out)
|
||||
{
|
||||
double y = ada->weights[ada->num_weights - 1]; // assign bias value
|
||||
|
||||
for (int i = 0; i < ada->num_weights - 1; i++)
|
||||
y += x[i] * ada->weights[i];
|
||||
for (int i = 0; i < ada->num_weights - 1; i++) y += x[i] * ada->weights[i];
|
||||
|
||||
if (out) // if out variable is not NULL
|
||||
*out = y;
|
||||
@ -261,8 +258,7 @@ void test2(double eta)
|
||||
|
||||
double **X = (double **)malloc(N * sizeof(double *));
|
||||
int *Y = (int *)malloc(N * sizeof(int)); // corresponding y-values
|
||||
for (int i = 0; i < N; i++)
|
||||
X[i] = (double *)malloc(2 * sizeof(double));
|
||||
for (int i = 0; i < N; i++) X[i] = (double *)malloc(2 * sizeof(double));
|
||||
|
||||
// generate sample points in the interval
|
||||
// [-range2/100 , (range2-1)/100]
|
||||
@ -300,8 +296,7 @@ void test2(double eta)
|
||||
printf(" ...passed\n");
|
||||
}
|
||||
|
||||
for (int i = 0; i < N; i++)
|
||||
free(X[i]);
|
||||
for (int i = 0; i < N; i++) free(X[i]);
|
||||
free(X);
|
||||
free(Y);
|
||||
delete_adaline(&ada);
|
||||
@ -326,8 +321,7 @@ void test3(double eta)
|
||||
|
||||
double **X = (double **)malloc(N * sizeof(double *));
|
||||
int *Y = (int *)malloc(N * sizeof(int)); // corresponding y-values
|
||||
for (int i = 0; i < N; i++)
|
||||
X[i] = (double *)malloc(6 * sizeof(double));
|
||||
for (int i = 0; i < N; i++) X[i] = (double *)malloc(6 * sizeof(double));
|
||||
|
||||
// generate sample points in the interval
|
||||
// [-range2/100 , (range2-1)/100]
|
||||
@ -374,8 +368,7 @@ void test3(double eta)
|
||||
printf(" ...passed\n");
|
||||
}
|
||||
|
||||
for (int i = 0; i < N; i++)
|
||||
free(X[i]);
|
||||
for (int i = 0; i < N; i++) free(X[i]);
|
||||
free(X);
|
||||
free(Y);
|
||||
delete_adaline(&ada);
|
||||
|
@ -47,7 +47,6 @@ void propagate(Contour *head)
|
||||
|
||||
void print(Contour *head)
|
||||
{
|
||||
|
||||
Contour *temp = head;
|
||||
while (temp != NULL)
|
||||
{
|
||||
@ -62,7 +61,6 @@ void print(Contour *head)
|
||||
|
||||
int main(int argc, char const *argv[])
|
||||
{
|
||||
|
||||
head = NULL;
|
||||
|
||||
int start_num, end_num, levels;
|
||||
|
@ -25,8 +25,7 @@ int main()
|
||||
temp = temp / 10;
|
||||
}
|
||||
}
|
||||
for (i = counter; i >= 0; i--)
|
||||
printf("%d", a[i]);
|
||||
for (i = counter; i >= 0; i--) printf("%d", a[i]);
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
@ -107,8 +107,7 @@ int main(int argc, char *argv[])
|
||||
large_num *result = new_number();
|
||||
|
||||
clock_t start_time = clock();
|
||||
for (i = 2; i <= number; i++)
|
||||
/* Multiply every number from 2 thru N */
|
||||
for (i = 2; i <= number; i++) /* Multiply every number from 2 thru N */
|
||||
multiply(result, i);
|
||||
double time_taken = (clock() - start_time) * (double)1e3 / CLOCKS_PER_SEC;
|
||||
// time_taken = (clock() - start_time) / (double) CLOCKS_PER_SEC;
|
||||
|
@ -2,7 +2,6 @@
|
||||
|
||||
int main()
|
||||
{
|
||||
|
||||
int a[16500], T;
|
||||
long long int i, j;
|
||||
|
||||
|
@ -14,7 +14,8 @@ int main()
|
||||
printf("Input a number, this is the bigger bound of the lerp:\n");
|
||||
scanf("%f", &finish);
|
||||
|
||||
printf("Input a number, this is in how many steps you want to divide the "
|
||||
printf(
|
||||
"Input a number, this is in how many steps you want to divide the "
|
||||
"lerp:\n");
|
||||
scanf("%f", &steps);
|
||||
|
||||
|
@ -20,7 +20,6 @@ void longestSub(int *ARRAY, int ARRAY_LENGTH, int **RESULT, int *RESULT_LENGTH)
|
||||
{
|
||||
if (ARRAY[i] < PIVOT)
|
||||
{
|
||||
|
||||
TEMPORARY_ARRAY_LENGTH = 0;
|
||||
TEMPORARY_ARRAY = NULL;
|
||||
|
||||
@ -28,7 +27,6 @@ void longestSub(int *ARRAY, int ARRAY_LENGTH, int **RESULT, int *RESULT_LENGTH)
|
||||
{
|
||||
if (ARRAY[j] >= ARRAY[i])
|
||||
{
|
||||
|
||||
TEMPORARY_ARRAY_LENGTH++;
|
||||
TEMPORARY_ARRAY = (int *)realloc(
|
||||
TEMPORARY_ARRAY,
|
||||
@ -41,7 +39,6 @@ void longestSub(int *ARRAY, int ARRAY_LENGTH, int **RESULT, int *RESULT_LENGTH)
|
||||
&TEMPORARY_ARRAY, &TEMPORARY_ARRAY_LENGTH);
|
||||
if (LONGEST_SUB_LENGTH < TEMPORARY_ARRAY_LENGTH + 1)
|
||||
{
|
||||
|
||||
LONGEST_SUB_LENGTH = TEMPORARY_ARRAY_LENGTH + 1;
|
||||
LONGEST_SUB = (int *)realloc(
|
||||
LONGEST_SUB, LONGEST_SUB_LENGTH * sizeof(int));
|
||||
@ -57,7 +54,6 @@ void longestSub(int *ARRAY, int ARRAY_LENGTH, int **RESULT, int *RESULT_LENGTH)
|
||||
TEMPORARY_ARRAY_LENGTH = 0;
|
||||
for (i = 1; i < ARRAY_LENGTH; i++)
|
||||
{
|
||||
|
||||
if (ARRAY[i] >= PIVOT)
|
||||
{
|
||||
TEMPORARY_ARRAY_LENGTH++;
|
||||
@ -71,7 +67,6 @@ void longestSub(int *ARRAY, int ARRAY_LENGTH, int **RESULT, int *RESULT_LENGTH)
|
||||
&TEMPORARY_ARRAY_LENGTH);
|
||||
if (TEMPORARY_ARRAY_LENGTH + 1 > LONGEST_SUB_LENGTH)
|
||||
{
|
||||
|
||||
LONGEST_SUB_LENGTH = TEMPORARY_ARRAY_LENGTH + 1;
|
||||
LONGEST_SUB =
|
||||
(int *)realloc(LONGEST_SUB, LONGEST_SUB_LENGTH * sizeof(int));
|
||||
@ -86,7 +81,6 @@ void longestSub(int *ARRAY, int ARRAY_LENGTH, int **RESULT, int *RESULT_LENGTH)
|
||||
|
||||
int main()
|
||||
{
|
||||
|
||||
int EXAMPLE_LENGTH = 8;
|
||||
int EXAMPLE[] = {18, 2, 15, 4, 30, 0, 11, 12};
|
||||
|
||||
@ -96,8 +90,7 @@ int main()
|
||||
longestSub(EXAMPLE, EXAMPLE_LENGTH, &RESULT, &RESULT_LENGTH);
|
||||
|
||||
printf("Longest Sub Sequence length: %d and it's:\n", RESULT_LENGTH);
|
||||
for (i = 0; i < RESULT_LENGTH; i++)
|
||||
printf("%d ", RESULT[i]);
|
||||
for (i = 0; i < RESULT_LENGTH; i++) printf("%d ", RESULT[i]);
|
||||
printf("\n");
|
||||
|
||||
return 0;
|
||||
|
@ -62,7 +62,8 @@ int main()
|
||||
struct pid controller = {.lastError = 0, .integral = 0};
|
||||
|
||||
// Take the controller gains from the user
|
||||
printf("Please enter controller gains in format kP, kI, KD. For example, "
|
||||
printf(
|
||||
"Please enter controller gains in format kP, kI, KD. For example, "
|
||||
"\"1.2 2.1 3.2\"\n> ");
|
||||
scanf("%f %f %f", &controller.kP, &controller.kI, &controller.kD);
|
||||
printf("Using kP: %f, kI: %f, kD: %f\n", controller.kP, controller.kI,
|
||||
|
@ -45,7 +45,6 @@ void destroy(Range);
|
||||
*/
|
||||
int main()
|
||||
{
|
||||
|
||||
int n = 0; /* for user input */
|
||||
|
||||
printf("\t\tPrim factoriziation\n\n");
|
||||
|
@ -83,8 +83,7 @@ int main()
|
||||
scanf("%d%d%d", &N, &R, &C);
|
||||
int a[M], i, j;
|
||||
for (i = 0; i < N; i++)
|
||||
for (j = 0; j < N; j++)
|
||||
scanf("%d", &a[i * N + j]);
|
||||
for (j = 0; j < N; j++) scanf("%d", &a[i * N + j]);
|
||||
|
||||
if (solve(a))
|
||||
print(a);
|
||||
|
@ -65,7 +65,6 @@ int main(void)
|
||||
printf("\n");
|
||||
for (i = 0; i < n; i++)
|
||||
{
|
||||
|
||||
printf("Enter Co-efficient Of Equations %d & Total --->>>\n", i + 1);
|
||||
for (j = 0; j <= n; j++)
|
||||
{
|
||||
|
@ -32,7 +32,6 @@ int main()
|
||||
p = 1.0;
|
||||
for (j = 0; j < n; j++)
|
||||
{
|
||||
|
||||
if (i != j)
|
||||
{
|
||||
p = p * (a - x[j]) / (x[i] - x[j]);
|
||||
|
@ -32,8 +32,7 @@ int lu_decomposition(double **A, double **L, double **U, int mat_size)
|
||||
{
|
||||
// Summation of L[i,j] * U[j,k]
|
||||
double lu_sum = 0.;
|
||||
for (j = 0; j < row; j++)
|
||||
lu_sum += L[row][j] * U[j][col];
|
||||
for (j = 0; j < row; j++) lu_sum += L[row][j] * U[j][col];
|
||||
|
||||
// Evaluate U[i,k]
|
||||
U[row][col] = A[row][col] - lu_sum;
|
||||
@ -53,8 +52,7 @@ int lu_decomposition(double **A, double **L, double **U, int mat_size)
|
||||
|
||||
// Summation of L[i,j] * U[j,k]
|
||||
double lu_sum = 0.;
|
||||
for (j = 0; j < row; j++)
|
||||
lu_sum += L[col][j] * U[j][row];
|
||||
for (j = 0; j < row; j++) lu_sum += L[col][j] * U[j][row];
|
||||
|
||||
// Evaluate U[i,k]
|
||||
L[col][row] = (A[col][row] - lu_sum) / U[row][row];
|
||||
|
@ -29,8 +29,7 @@ int main(int argc, char **argv)
|
||||
}
|
||||
putchar('\n');
|
||||
|
||||
for (i = 0; i < n; i++)
|
||||
sum = sum + a[i];
|
||||
for (i = 0; i < n; i++) sum = sum + a[i];
|
||||
|
||||
mean = sum / (float)n;
|
||||
printf("\nMean :");
|
||||
|
@ -25,8 +25,7 @@ void print_matrix(double **A, /**< matrix to print */
|
||||
{
|
||||
for (int row = 0; row < M; row++)
|
||||
{
|
||||
for (int col = 0; col < N; col++)
|
||||
printf("% 9.3g\t", A[row][col]);
|
||||
for (int col = 0; col < N; col++) printf("% 9.3g\t", A[row][col]);
|
||||
putchar('\n');
|
||||
}
|
||||
putchar('\n');
|
||||
@ -49,8 +48,7 @@ double vector_dot(double *a, double *b, int L)
|
||||
// parallelize on threads
|
||||
#pragma omp parallel for reduction(+ : mag)
|
||||
#endif
|
||||
for (i = 0; i < L; i++)
|
||||
mag += a[i] * b[i];
|
||||
for (i = 0; i < L; i++) mag += a[i] * b[i];
|
||||
|
||||
return mag;
|
||||
}
|
||||
@ -88,8 +86,7 @@ double *vector_proj(double *a, double *b, double *out, int L)
|
||||
// parallelize on threads
|
||||
#pragma omp for
|
||||
#endif
|
||||
for (i = 0; i < L; i++)
|
||||
out[i] = scalar * b[i];
|
||||
for (i = 0; i < L; i++) out[i] = scalar * b[i];
|
||||
|
||||
return out;
|
||||
}
|
||||
@ -112,8 +109,7 @@ double *vector_sub(double *a, /**< minuend */
|
||||
// parallelize on threads
|
||||
#pragma omp for
|
||||
#endif
|
||||
for (i = 0; i < L; i++)
|
||||
out[i] = a[i] - b[i];
|
||||
for (i = 0; i < L; i++) out[i] = a[i] - b[i];
|
||||
|
||||
return out;
|
||||
}
|
||||
@ -176,8 +172,7 @@ void qr_decompose(double **A, /**< input matrix to decompose */
|
||||
}
|
||||
for (j = 0; j < i; j++)
|
||||
{
|
||||
for (int k = 0; k < M; k++)
|
||||
col_vector2[k] = Q[k][j];
|
||||
for (int k = 0; k < M; k++) col_vector2[k] = Q[k][j];
|
||||
vector_proj(col_vector, col_vector2, col_vector2, M);
|
||||
vector_sub(tmp_vector, col_vector2, tmp_vector, M);
|
||||
}
|
||||
@ -187,16 +182,13 @@ void qr_decompose(double **A, /**< input matrix to decompose */
|
||||
// parallelize on threads
|
||||
#pragma omp for
|
||||
#endif
|
||||
for (j = 0; j < M; j++)
|
||||
Q[j][i] = tmp_vector[j] / mag;
|
||||
for (j = 0; j < M; j++) Q[j][i] = tmp_vector[j] / mag;
|
||||
|
||||
/* compute upper triangular values of R */
|
||||
for (int kk = 0; kk < M; kk++)
|
||||
col_vector[kk] = Q[kk][i];
|
||||
for (int kk = 0; kk < M; kk++) col_vector[kk] = Q[kk][i];
|
||||
for (int k = i; k < N; k++)
|
||||
{
|
||||
for (int kk = 0; kk < M; kk++)
|
||||
col_vector2[kk] = A[kk][k];
|
||||
for (int kk = 0; kk < M; kk++) col_vector2[kk] = A[kk][k];
|
||||
R[i][k] = vector_dot(col_vector, col_vector2, M);
|
||||
}
|
||||
}
|
||||
|
@ -6,11 +6,11 @@
|
||||
* \author [Krishna Vedala](https://github.com/kvedala)
|
||||
*/
|
||||
|
||||
#include "qr_decompose.h"
|
||||
#include <math.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <time.h>
|
||||
#include "qr_decompose.h"
|
||||
|
||||
/**
|
||||
* main function
|
||||
@ -24,7 +24,8 @@ int main(void)
|
||||
scanf("%u %u", &ROWS, &COLUMNS);
|
||||
if (ROWS < COLUMNS)
|
||||
{
|
||||
fprintf(stderr, "Number of rows must be greater than or equal to "
|
||||
fprintf(stderr,
|
||||
"Number of rows must be greater than or equal to "
|
||||
"number of columns.\n");
|
||||
return -1;
|
||||
}
|
||||
@ -36,8 +37,7 @@ int main(void)
|
||||
A[i] = (double *)malloc(COLUMNS * sizeof(double));
|
||||
|
||||
for (int i = 0; i < ROWS; i++)
|
||||
for (int j = 0; j < COLUMNS; j++)
|
||||
scanf("%lf", &A[i][j]);
|
||||
for (int j = 0; j < COLUMNS; j++) scanf("%lf", &A[i][j]);
|
||||
|
||||
print_matrix(A, ROWS, COLUMNS);
|
||||
|
||||
|
@ -5,12 +5,12 @@
|
||||
* method.
|
||||
* \author [Krishna Vedala](https://github.com/kvedala)
|
||||
*/
|
||||
#include "qr_decompose.h"
|
||||
#include <assert.h>
|
||||
#include <math.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <time.h>
|
||||
#include "qr_decompose.h"
|
||||
#ifdef _OPENMP
|
||||
#include <omp.h>
|
||||
#endif
|
||||
@ -72,8 +72,7 @@ double **mat_mul(double **A, double **B, double **OUT, int R1, int C1, int R2,
|
||||
for (int j = 0; j < C2; j++)
|
||||
{
|
||||
OUT[i][j] = 0.f;
|
||||
for (int k = 0; k < C1; k++)
|
||||
OUT[i][j] += A[i][k] * B[k][j];
|
||||
for (int k = 0; k < C1; k++) OUT[i][j] += A[i][k] * B[k][j];
|
||||
}
|
||||
return OUT;
|
||||
}
|
||||
@ -144,8 +143,7 @@ double eigen_values(double **A, double *eigen_vals, int mat_size,
|
||||
while (fabs(A[num_eigs][num_eigs - 1]) > EPSILON)
|
||||
{
|
||||
last_eig = A[num_eigs][num_eigs];
|
||||
for (int i = 0; i < rows; i++)
|
||||
A[i][i] -= last_eig; /* A - cI */
|
||||
for (int i = 0; i < rows; i++) A[i][i] -= last_eig; /* A - cI */
|
||||
qr_decompose(A, Q, R, rows, columns);
|
||||
|
||||
if (debug_print)
|
||||
@ -158,8 +156,7 @@ double eigen_values(double **A, double *eigen_vals, int mat_size,
|
||||
}
|
||||
|
||||
mat_mul(R, Q, A, columns, columns, rows, columns);
|
||||
for (int i = 0; i < rows; i++)
|
||||
A[i][i] += last_eig; /* A + cI */
|
||||
for (int i = 0; i < rows; i++) A[i][i] += last_eig; /* A + cI */
|
||||
}
|
||||
|
||||
/* store the converged eigen value */
|
||||
@ -214,8 +211,7 @@ void test1()
|
||||
|
||||
// The following steps are to convert a "double[][]" to "double **"
|
||||
double **A = (double **)malloc(mat_size * sizeof(double *));
|
||||
for (int i = 0; i < mat_size; i++)
|
||||
A[i] = X[i];
|
||||
for (int i = 0; i < mat_size; i++) A[i] = X[i];
|
||||
|
||||
printf("------- Test 1 -------\n");
|
||||
|
||||
@ -267,8 +263,7 @@ void test2()
|
||||
|
||||
// The following steps are to convert a "double[][]" to "double **"
|
||||
double **A = (double **)malloc(mat_size * sizeof(double *));
|
||||
for (int i = 0; i < mat_size; i++)
|
||||
A[i] = X[i];
|
||||
for (int i = 0; i < mat_size; i++) A[i] = X[i];
|
||||
|
||||
printf("------- Test 2 -------\n");
|
||||
|
||||
@ -341,12 +336,10 @@ int main(int argc, char **argv)
|
||||
|
||||
double dtime = eigen_values(A, eigen_vals, mat_size, 0);
|
||||
printf("Eigen vals: ");
|
||||
for (i = 0; i < mat_size; i++)
|
||||
printf("% 9.4g\t", eigen_vals[i]);
|
||||
for (i = 0; i < mat_size; i++) printf("% 9.4g\t", eigen_vals[i]);
|
||||
printf("\nTime taken to compute: % .4g sec\n", dtime);
|
||||
|
||||
for (int i = 0; i < mat_size; i++)
|
||||
free(A[i]);
|
||||
for (int i = 0; i < mat_size; i++) free(A[i]);
|
||||
free(A);
|
||||
free(eigen_vals);
|
||||
return 0;
|
||||
|
@ -4,7 +4,6 @@
|
||||
|
||||
int main()
|
||||
{
|
||||
|
||||
int *ARRAY = NULL, ARRAY_LENGTH, i, TEMPORARY_ELEMENT, isSorted = 0;
|
||||
float MEAN = 0, VARIANCE = 0, STAND;
|
||||
|
||||
@ -17,8 +16,7 @@ int main()
|
||||
ARRAY[i] = rand() % 100;
|
||||
|
||||
printf("Random Numbers Generated are :\n"); // We display them
|
||||
for (i = 0; i < ARRAY_LENGTH; i++)
|
||||
printf("%d ", ARRAY[i]);
|
||||
for (i = 0; i < ARRAY_LENGTH; i++) printf("%d ", ARRAY[i]);
|
||||
|
||||
printf("\nSorted Data: "); // Then we sort it using Bubble Sort..
|
||||
|
||||
|
@ -85,8 +85,7 @@ int print_number(uint8_t *number, uint8_t N, int8_t num_digits_to_print)
|
||||
uint8_t end_pos;
|
||||
|
||||
/* skip all initial zeros */
|
||||
while (number[start_pos] == 0)
|
||||
start_pos--;
|
||||
while (number[start_pos] == 0) start_pos--;
|
||||
|
||||
/* if end_pos < 0, print all digits */
|
||||
if (num_digits_to_print < 0)
|
||||
@ -99,8 +98,7 @@ int print_number(uint8_t *number, uint8_t N, int8_t num_digits_to_print)
|
||||
return -1;
|
||||
}
|
||||
|
||||
for (int i = start_pos; i >= end_pos; i--)
|
||||
putchar(number[i] + 0x30);
|
||||
for (int i = start_pos; i >= end_pos; i--) putchar(number[i] + 0x30);
|
||||
|
||||
putchar('\n');
|
||||
|
||||
|
@ -54,8 +54,7 @@ int main(int argc, char **argv)
|
||||
}
|
||||
|
||||
printf("2^%d = ", N);
|
||||
for (int i = MAX_NUM_DIGITS - 1; i >= 0; i--)
|
||||
putchar(digits[i] + 0x30);
|
||||
for (int i = MAX_NUM_DIGITS - 1; i >= 0; i--) putchar(digits[i] + 0x30);
|
||||
printf("\n\t Sum: %d\t Num. digits: %lu\n", sum, MAX_NUM_DIGITS);
|
||||
|
||||
free(digits);
|
||||
|
@ -122,7 +122,8 @@ int main(int argc, char **argv)
|
||||
}
|
||||
}
|
||||
|
||||
printf("Total number of Sundays that happened on the 1st of a month in the "
|
||||
printf(
|
||||
"Total number of Sundays that happened on the 1st of a month in the "
|
||||
"last century: %d\n",
|
||||
count_sundays);
|
||||
|
||||
|
@ -38,8 +38,7 @@ void shell_sort(char data[][MAX_NAME_LEN], int LEN)
|
||||
}
|
||||
}
|
||||
#ifdef DEBUG
|
||||
for (i = 0; i < LEN; i++)
|
||||
printf("%s\t", data[i]);
|
||||
for (i = 0; i < LEN; i++) printf("%s\t", data[i]);
|
||||
#endif
|
||||
}
|
||||
|
||||
@ -63,8 +62,7 @@ void lazy_sort(char data[][MAX_NAME_LEN], int LEN)
|
||||
}
|
||||
}
|
||||
#ifdef DEBUG
|
||||
for (i = 0; i < LEN; i++)
|
||||
printf("%s\t", data[i]);
|
||||
for (i = 0; i < LEN; i++) printf("%s\t", data[i]);
|
||||
#endif
|
||||
}
|
||||
|
||||
|
@ -117,7 +117,8 @@ int main(int argc, char **argv)
|
||||
}
|
||||
|
||||
printf("Time taken: %.4g s\n", total_duration);
|
||||
printf("Sum of numbers that cannot be represented as sum of two abundant "
|
||||
printf(
|
||||
"Sum of numbers that cannot be represented as sum of two abundant "
|
||||
"numbers : %lu\n",
|
||||
sum);
|
||||
|
||||
|
@ -182,7 +182,8 @@ int main(int argc, char **argv)
|
||||
printf("Time taken for final sum: %.4g ms\nTotal Time taken: %.4g ms\n",
|
||||
t22, t1 + t22);
|
||||
printf("Memory used: %lu bytes\n", MAX_N >> 3);
|
||||
printf("Sum of numbers that cannot be represented as sum of two abundant "
|
||||
printf(
|
||||
"Sum of numbers that cannot be represented as sum of two abundant "
|
||||
"numbers : %lu\n",
|
||||
sum);
|
||||
|
||||
|
@ -60,11 +60,9 @@ int print_number(unsigned char *number, int N)
|
||||
int start_pos = N - 1;
|
||||
|
||||
/* skip all initial zeros */
|
||||
while (number[start_pos] == 0)
|
||||
start_pos--;
|
||||
while (number[start_pos] == 0) start_pos--;
|
||||
|
||||
for (int i = start_pos; i >= 0; i--)
|
||||
putchar(number[i] + 0x30);
|
||||
for (int i = start_pos; i >= 0; i--) putchar(number[i] + 0x30);
|
||||
|
||||
return 0;
|
||||
}
|
||||
@ -73,8 +71,7 @@ int print_number(unsigned char *number, int N)
|
||||
unsigned int get_digits(unsigned char *number)
|
||||
{
|
||||
unsigned int digits = MAX_DIGITS;
|
||||
while (number[digits] == 0)
|
||||
digits--;
|
||||
while (number[digits] == 0) digits--;
|
||||
return digits;
|
||||
}
|
||||
|
||||
|
@ -40,8 +40,7 @@ int main()
|
||||
int n = sizeof(arr) / sizeof(arr[0]);
|
||||
|
||||
printf("Array: ");
|
||||
for (int i = 0; i < n; i++)
|
||||
printf("%d ", arr[i]);
|
||||
for (int i = 0; i < n; i++) printf("%d ", arr[i]);
|
||||
printf("\nEnter the number to be searched: ");
|
||||
scanf("%d", &x); /* Element to be searched */
|
||||
|
||||
|
@ -80,8 +80,7 @@ int main()
|
||||
int x; // element to be searched
|
||||
scanf("%d %d %d\n", &n, &m, &x);
|
||||
int **mat = (int **)malloc(n * sizeof(int *));
|
||||
for (x = 0; x < n; x++)
|
||||
mat[x] = (int *)malloc(m * sizeof(int));
|
||||
for (x = 0; x < n; x++) mat[x] = (int *)malloc(m * sizeof(int));
|
||||
|
||||
for (int i = 0; i < n; i++)
|
||||
{
|
||||
@ -92,8 +91,7 @@ int main()
|
||||
}
|
||||
modifiedBinarySearch(mat, n, m, x);
|
||||
|
||||
for (x = 0; x < n; x++)
|
||||
free(mat[x]);
|
||||
for (x = 0; x < n; x++) free(mat[x]);
|
||||
free(mat);
|
||||
return 0;
|
||||
}
|
||||
|
@ -9,11 +9,9 @@ void computeArray(char *pattern, int size, int arr[NUM_OF_CHARS])
|
||||
{
|
||||
int i;
|
||||
|
||||
for (i = 0; i < NUM_OF_CHARS; i++)
|
||||
arr[i] = -1;
|
||||
for (i = 0; i < NUM_OF_CHARS; i++) arr[i] = -1;
|
||||
/* Fill the actual value of last occurrence of a character */
|
||||
for (i = 0; i < size; i++)
|
||||
arr[(int)pattern[i]] = i;
|
||||
for (i = 0; i < size; i++) arr[(int)pattern[i]] = i;
|
||||
}
|
||||
/* Boyer Moore Search algorithm */
|
||||
void boyer_moore_search(char *str, char *pattern)
|
||||
@ -27,8 +25,7 @@ void boyer_moore_search(char *str, char *pattern)
|
||||
while (shift <= (n - m))
|
||||
{
|
||||
int j = m - 1;
|
||||
while (j >= 0 && pattern[j] == str[shift + j])
|
||||
j--;
|
||||
while (j >= 0 && pattern[j] == str[shift + j]) j--;
|
||||
if (j < 0)
|
||||
{
|
||||
printf("--Pattern is found at: %d\n", shift);
|
||||
|
@ -13,8 +13,7 @@ void rabin_karp_search(char *str, char *pattern, int d, int q)
|
||||
int hash_p = 0; /* hash value for pattern */
|
||||
|
||||
/* h = pow(d, len_pat - 1) % q */
|
||||
for (i = 0; i < len_pat - 1; i++)
|
||||
h = d * h % q;
|
||||
for (i = 0; i < len_pat - 1; i++) h = d * h % q;
|
||||
/* Calculating hashing of pattern and the 1st window of text */
|
||||
for (i = 0; i < len_pat; i++)
|
||||
{
|
||||
|
@ -6,7 +6,6 @@ int ternarySearch(int l, int r, int key, int ar[])
|
||||
{
|
||||
if (r >= l)
|
||||
{
|
||||
|
||||
// Find the mid1 and mid2
|
||||
int mid1 = l + (r - l) / 3;
|
||||
int mid2 = r - (r - l) / 3;
|
||||
@ -28,19 +27,16 @@ int ternarySearch(int l, int r, int key, int ar[])
|
||||
|
||||
if (key < ar[mid1])
|
||||
{
|
||||
|
||||
// The key lies in between l and mid1
|
||||
return ternarySearch(l, mid1 - 1, key, ar);
|
||||
}
|
||||
else if (key > ar[mid2])
|
||||
{
|
||||
|
||||
// The key lies in between mid2 and r
|
||||
return ternarySearch(mid2 + 1, r, key, ar);
|
||||
}
|
||||
else
|
||||
{
|
||||
|
||||
// The key lies in between mid1 and mid2
|
||||
return ternarySearch(mid1 + 1, mid2 - 1, key, ar);
|
||||
}
|
||||
|
@ -5,7 +5,6 @@
|
||||
/*Displays the array, passed to this method*/
|
||||
void display(int *arr, int n)
|
||||
{
|
||||
|
||||
int i;
|
||||
for (i = 0; i < n; i++)
|
||||
{
|
||||
@ -33,8 +32,7 @@ void bead_sort(int *a, int len)
|
||||
|
||||
/* mark the beads */
|
||||
for (i = 0; i < len; i++)
|
||||
for (j = 0; j < a[i]; j++)
|
||||
BEAD(i, j) = 1;
|
||||
for (j = 0; j < a[i]; j++) BEAD(i, j) = 1;
|
||||
|
||||
for (j = 0; j < max; j++)
|
||||
{
|
||||
@ -45,8 +43,7 @@ void bead_sort(int *a, int len)
|
||||
BEAD(i, j) = 0;
|
||||
}
|
||||
/* mark bottom sum beads */
|
||||
for (i = len - sum; i < len; i++)
|
||||
BEAD(i, j) = 1;
|
||||
for (i = len - sum; i < len; i++) BEAD(i, j) = 1;
|
||||
}
|
||||
|
||||
for (i = 0; i < len; i++)
|
||||
|
@ -26,8 +26,7 @@ void shuffle(int *a, int n)
|
||||
|
||||
void sort(int *a, int n)
|
||||
{
|
||||
while (!check_sorted(a, n))
|
||||
shuffle(a, n);
|
||||
while (!check_sorted(a, n)) shuffle(a, n);
|
||||
}
|
||||
|
||||
int main()
|
||||
@ -40,7 +39,6 @@ int main()
|
||||
scanf("%d", &numbers[i]);
|
||||
}
|
||||
sort(numbers, 6);
|
||||
for (i = 0; i < 6; i++)
|
||||
printf("%d ", numbers[i]);
|
||||
for (i = 0; i < 6; i++) printf("%d ", numbers[i]);
|
||||
printf("\n");
|
||||
}
|
||||
|
@ -5,7 +5,6 @@
|
||||
/*Displays the array, passed to this method*/
|
||||
void display(int *arr, int n)
|
||||
{
|
||||
|
||||
int i;
|
||||
for (i = 0; i < n; i++)
|
||||
{
|
||||
@ -18,7 +17,6 @@ void display(int *arr, int n)
|
||||
/*Swap function to swap two values*/
|
||||
void swap(int *first, int *second)
|
||||
{
|
||||
|
||||
int temp = *first;
|
||||
*first = *second;
|
||||
*second = temp;
|
||||
@ -30,7 +28,6 @@ void swap(int *first, int *second)
|
||||
*/
|
||||
void bubbleSort(int *arr, int size)
|
||||
{
|
||||
|
||||
for (int i = 0; i < size - 1; i++)
|
||||
{
|
||||
for (int j = 0; j < size - 1 - i; j++)
|
||||
|
@ -6,7 +6,6 @@
|
||||
|
||||
int main()
|
||||
{
|
||||
|
||||
int i, arraySort[MAX] = {0}, isSort = FALSE, changePlace;
|
||||
|
||||
/* For example
|
||||
|
@ -79,7 +79,6 @@ void BucketSort(int arr[])
|
||||
node = buckets[i];
|
||||
while (node)
|
||||
{
|
||||
|
||||
// precondition for avoiding out of bounds by the array
|
||||
assert(j < NARRAY);
|
||||
arr[j++] = node->data;
|
||||
|
@ -25,8 +25,7 @@ void sort(int *numbers, int size)
|
||||
void display(int *array, int n)
|
||||
{
|
||||
int i;
|
||||
for (i = 0; i < n; ++i)
|
||||
printf("%d ", array[i]);
|
||||
for (i = 0; i < n; ++i) printf("%d ", array[i]);
|
||||
printf("\n");
|
||||
}
|
||||
|
||||
@ -36,8 +35,7 @@ int main()
|
||||
int *numbers = malloc(size * sizeof(int));
|
||||
printf("Insert %d unsorted numbers: \n", size);
|
||||
int i;
|
||||
for (i = 0; i < size; ++i)
|
||||
scanf("%d", &numbers[i]);
|
||||
for (i = 0; i < size; ++i) scanf("%d", &numbers[i]);
|
||||
printf("Initial array: ");
|
||||
display(numbers, size);
|
||||
sort(numbers, size);
|
||||
|
@ -28,8 +28,7 @@ int main()
|
||||
int *b = (int *)malloc((l + 1) * sizeof(int));
|
||||
memset(b, 0, (l + 1) * sizeof(b[0]));
|
||||
|
||||
for (i = 0; i < n; i++)
|
||||
b[a[i]]++; // hashing number to array index
|
||||
for (i = 0; i < n; i++) b[a[i]]++; // hashing number to array index
|
||||
|
||||
for (i = 0; i < (l + 1); i++) // unstable , stabilized by prefix sum array
|
||||
{
|
||||
|
@ -5,7 +5,6 @@
|
||||
// Displays the array, passed to this method
|
||||
void display(int *arr, int n)
|
||||
{
|
||||
|
||||
int i;
|
||||
for (i = 0; i < n; i++)
|
||||
{
|
||||
@ -18,7 +17,6 @@ void display(int *arr, int n)
|
||||
// Swap function to swap two values
|
||||
void swap(int *first, int *second)
|
||||
{
|
||||
|
||||
int temp = *first;
|
||||
*first = *second;
|
||||
*second = temp;
|
||||
@ -49,8 +47,7 @@ void cycleSort(int *arr, int n)
|
||||
continue;
|
||||
|
||||
// ignore all duplicate elements
|
||||
while (item == arr[pos])
|
||||
pos += 1;
|
||||
while (item == arr[pos]) pos += 1;
|
||||
|
||||
// put the item to it's right position
|
||||
if (pos != cycle_start)
|
||||
@ -70,8 +67,7 @@ void cycleSort(int *arr, int n)
|
||||
pos += 1;
|
||||
|
||||
// ignore all duplicate elements
|
||||
while (item == arr[pos])
|
||||
pos += 1;
|
||||
while (item == arr[pos]) pos += 1;
|
||||
|
||||
// put the item to it's right position
|
||||
if (item != arr[pos])
|
||||
|
@ -23,8 +23,7 @@ void sort(int *numbers, int size)
|
||||
void display(int *array, int n)
|
||||
{
|
||||
int i;
|
||||
for (i = 0; i < n; ++i)
|
||||
printf("%d ", array[i]);
|
||||
for (i = 0; i < n; ++i) printf("%d ", array[i]);
|
||||
printf("\n");
|
||||
}
|
||||
|
||||
@ -34,8 +33,7 @@ int main()
|
||||
int i;
|
||||
int *numbers = malloc(size * sizeof(int));
|
||||
printf("Insert %d unsorted numbers: \n", size);
|
||||
for (i = 0; i < size; ++i)
|
||||
scanf("%d", &numbers[i]);
|
||||
for (i = 0; i < size; ++i) scanf("%d", &numbers[i]);
|
||||
printf("Initial array: ");
|
||||
display(numbers, size);
|
||||
sort(numbers, size);
|
||||
|
@ -47,8 +47,7 @@ void merge(int a[], int l, int r, int n) // To merge //
|
||||
};
|
||||
}
|
||||
|
||||
for (c = l; c < r - l + 1; c++)
|
||||
a[c] = b[c];
|
||||
for (c = l; c < r - l + 1; c++) a[c] = b[c];
|
||||
}
|
||||
|
||||
void merge_sort(int *a, int n, int l, int r)
|
||||
@ -75,14 +74,12 @@ int main(void)
|
||||
a = (int *)malloc(n * sizeof(int));
|
||||
for (i = 0; i < n; i++)
|
||||
{
|
||||
|
||||
scanf("%d", &a[i]);
|
||||
}
|
||||
|
||||
merge_sort(a, n, 0, n - 1);
|
||||
for (i = 0; i < n; i++)
|
||||
{
|
||||
|
||||
printf(" %d", a[i]);
|
||||
}
|
||||
|
||||
|
@ -44,17 +44,14 @@ void mergesort(int x[], int n)
|
||||
else
|
||||
temp[k++] = x[j++];
|
||||
|
||||
while (i <= ub1)
|
||||
temp[k++] = x[i++];
|
||||
while (i <= ub1) temp[k++] = x[i++];
|
||||
|
||||
while (j <= ub2)
|
||||
temp[k++] = x[j++];
|
||||
while (j <= ub2) temp[k++] = x[j++];
|
||||
|
||||
lb1 = ub2 + 1;
|
||||
}
|
||||
|
||||
for (i = 0; i <= ub2; i++)
|
||||
x[i] = temp[i];
|
||||
for (i = 0; i <= ub2; i++) x[i] = temp[i];
|
||||
|
||||
size = size * 2;
|
||||
|
||||
@ -66,8 +63,7 @@ void mergesort(int x[], int n)
|
||||
void show(int x[], int n)
|
||||
{
|
||||
int i;
|
||||
for (i = 0; i < n; i++)
|
||||
printf("%d ", x[i]);
|
||||
for (i = 0; i < n; i++) printf("%d ", x[i]);
|
||||
printf("\n\n");
|
||||
}
|
||||
|
||||
@ -78,14 +74,12 @@ int main() // main function
|
||||
printf("Enter the number of elements: ");
|
||||
scanf("%d", &n);
|
||||
printf("Enter the elements:\n");
|
||||
for (i = 0; i < n; i++)
|
||||
scanf("%d", &x[i]);
|
||||
for (i = 0; i < n; i++) scanf("%d", &x[i]);
|
||||
|
||||
mergesort(x, n);
|
||||
|
||||
printf("Sorted array is as shown:\n");
|
||||
for (i = 0; i < n; i++)
|
||||
printf("%d ", x[i]);
|
||||
for (i = 0; i < n; i++) printf("%d ", x[i]);
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
@ -294,8 +294,7 @@ void insert2(char *s)
|
||||
void cleanup2()
|
||||
{
|
||||
int i;
|
||||
for (i = 0; i < freen; i++)
|
||||
free(freearr[i]);
|
||||
for (i = 0; i < freen; i++) free(freearr[i]);
|
||||
}
|
||||
|
||||
// Search Algorithms
|
||||
@ -387,7 +386,6 @@ void nearsearch(Tptr p, char *s, int d)
|
||||
|
||||
int main(int argc, char *argv[])
|
||||
{
|
||||
|
||||
char *arr[NUMBER_OF_STRING] = {"apple", "cat", "boy"};
|
||||
|
||||
ssort1main(arr, NUMBER_OF_STRING);
|
||||
|
@ -48,8 +48,7 @@ void partitionSort(int arr[], int low, int high)
|
||||
void printArray(int arr[], int n)
|
||||
{
|
||||
int i;
|
||||
for (i = 0; i < n; i++)
|
||||
printf("%d ", arr[i]);
|
||||
for (i = 0; i < n; i++) printf("%d ", arr[i]);
|
||||
printf("\n");
|
||||
}
|
||||
|
||||
|
@ -4,7 +4,6 @@
|
||||
/*Displays the array, passed to this method*/
|
||||
void display(int arr[], int n)
|
||||
{
|
||||
|
||||
int i;
|
||||
for (i = 0; i < n; i++)
|
||||
{
|
||||
@ -17,7 +16,6 @@ void display(int arr[], int n)
|
||||
/*Swap function to swap two values*/
|
||||
void swap(int *first, int *second)
|
||||
{
|
||||
|
||||
int temp = *first;
|
||||
*first = *second;
|
||||
*second = temp;
|
||||
@ -32,7 +30,6 @@ void swap(int *first, int *second)
|
||||
*/
|
||||
int partition(int arr[], int lower, int upper)
|
||||
{
|
||||
|
||||
int i = (lower - 1);
|
||||
|
||||
int pivot = arr[upper]; // Selects last element as the pivot value
|
||||
@ -40,7 +37,6 @@ int partition(int arr[], int lower, int upper)
|
||||
int j;
|
||||
for (j = lower; j < upper; j++)
|
||||
{
|
||||
|
||||
if (arr[j] <= pivot)
|
||||
{ // if current element is smaller than the pivot
|
||||
|
||||
@ -49,8 +45,8 @@ int partition(int arr[], int lower, int upper)
|
||||
}
|
||||
}
|
||||
|
||||
swap(&arr[i + 1], &arr[upper]); // places the last element i.e, the pivot to
|
||||
// its correct position
|
||||
swap(&arr[i + 1], &arr[upper]); // places the last element i.e, the pivot
|
||||
// to its correct position
|
||||
|
||||
return (i + 1);
|
||||
}
|
||||
@ -62,10 +58,8 @@ int partition(int arr[], int lower, int upper)
|
||||
*/
|
||||
void quickSort(int arr[], int lower, int upper)
|
||||
{
|
||||
|
||||
if (upper > lower)
|
||||
{
|
||||
|
||||
// partitioning index is returned by the partition method , partition
|
||||
// element is at its correct poition
|
||||
|
||||
@ -79,7 +73,6 @@ void quickSort(int arr[], int lower, int upper)
|
||||
|
||||
int main()
|
||||
{
|
||||
|
||||
int n;
|
||||
printf("Enter size of array:\n");
|
||||
scanf("%d", &n); // E.g. 8
|
||||
|
@ -48,8 +48,7 @@ void RadixSort(int a[], int n)
|
||||
}
|
||||
divisor *= 10;
|
||||
|
||||
for (i = 0; i < n; i++)
|
||||
printf("%d ", a[i]);
|
||||
for (i = 0; i < n; i++) printf("%d ", a[i]);
|
||||
printf("\n");
|
||||
}
|
||||
}
|
||||
@ -66,8 +65,7 @@ int main()
|
||||
}
|
||||
RadixSort(a, n);
|
||||
printf("The sorted elements are :: ");
|
||||
for (i = 0; i < n; i++)
|
||||
printf("%d ", a[i]);
|
||||
for (i = 0; i < n; i++) printf("%d ", a[i]);
|
||||
printf("\n");
|
||||
return 0;
|
||||
}
|
||||
|
@ -23,13 +23,11 @@ void countSort(int *arr, int n, int place)
|
||||
int *output = (int *)malloc(n * sizeof(int));
|
||||
|
||||
// Store count of occurences in freq[]
|
||||
for (i = 0; i < n; i++)
|
||||
freq[(arr[i] / place) % range]++;
|
||||
for (i = 0; i < n; i++) freq[(arr[i] / place) % range]++;
|
||||
|
||||
// Change freq[i] so that it contains the actual position of the digit in
|
||||
// output[]
|
||||
for (i = 1; i < range; i++)
|
||||
freq[i] += freq[i - 1];
|
||||
for (i = 1; i < range; i++) freq[i] += freq[i - 1];
|
||||
|
||||
// Build the output array
|
||||
for (i = n - 1; i >= 0; i--)
|
||||
@ -40,8 +38,7 @@ void countSort(int *arr, int n, int place)
|
||||
|
||||
// Copy the output array to arr[], so it contains numbers according to the
|
||||
// current digit
|
||||
for (i = 0; i < n; i++)
|
||||
arr[i] = output[i];
|
||||
for (i = 0; i < n; i++) arr[i] = output[i];
|
||||
free(output);
|
||||
}
|
||||
|
||||
@ -64,8 +61,7 @@ void radixsort2(int *arr, int n,
|
||||
|
||||
void display(int *arr, int N)
|
||||
{
|
||||
for (int i = 0; i < N; i++)
|
||||
printf("%d, ", arr[i]);
|
||||
for (int i = 0; i < N; i++) printf("%d, ", arr[i]);
|
||||
putchar('\n');
|
||||
}
|
||||
|
||||
|
@ -5,7 +5,6 @@
|
||||
/*Displays the array, passed to this method*/
|
||||
void display(int *arr, int n)
|
||||
{
|
||||
|
||||
int i;
|
||||
for (i = 0; i < n; i++)
|
||||
{
|
||||
@ -18,7 +17,6 @@ void display(int *arr, int n)
|
||||
/*Swap function to swap two values*/
|
||||
void swap(int *first, int *second)
|
||||
{
|
||||
|
||||
int temp = *first;
|
||||
*first = *second;
|
||||
*second = temp;
|
||||
@ -30,7 +28,6 @@ void swap(int *first, int *second)
|
||||
*/
|
||||
void selectionSort(int *arr, int size)
|
||||
{
|
||||
|
||||
for (int i = 0; i < size; i++)
|
||||
{
|
||||
int min_index = i;
|
||||
|
@ -31,11 +31,9 @@ int main()
|
||||
scanf("%d", &n);
|
||||
int *arr = (int *)malloc(n * sizeof(int));
|
||||
int i;
|
||||
for (i = 0; i < n; i++)
|
||||
scanf("%d ", &arr[i]);
|
||||
for (i = 0; i < n; i++) scanf("%d ", &arr[i]);
|
||||
shakersort(arr, n);
|
||||
for (i = 0; i < n; i++)
|
||||
printf("%d ", arr[i]);
|
||||
for (i = 0; i < n; i++) printf("%d ", arr[i]);
|
||||
free(arr);
|
||||
return 0;
|
||||
}
|
||||
|
@ -4,7 +4,8 @@
|
||||
|
||||
#define ELEMENT_NR 20000
|
||||
#define ARRAY_LEN(x) (sizeof(x) / sizeof((x)[0]))
|
||||
const char *notation = "Shell Sort Big O Notation:\
|
||||
const char *notation =
|
||||
"Shell Sort Big O Notation:\
|
||||
\n--> Best Case: O(n log(n)) \
|
||||
\n--> Average Case: depends on gap sequence \
|
||||
\n--> Worst Case: O(n)";
|
||||
@ -13,8 +14,7 @@ void show_data(int arr[], int len)
|
||||
{
|
||||
int i;
|
||||
|
||||
for (i = 0; i < len; i++)
|
||||
printf("%3d ", arr[i]);
|
||||
for (i = 0; i < len; i++) printf("%3d ", arr[i]);
|
||||
printf("\n");
|
||||
}
|
||||
|
||||
@ -47,8 +47,7 @@ int main(int argc, char *argv[])
|
||||
double time_spent;
|
||||
|
||||
srand(time(NULL));
|
||||
for (i = 0; i < ELEMENT_NR; i++)
|
||||
array[i] = rand() % range + 1;
|
||||
for (i = 0; i < ELEMENT_NR; i++) array[i] = rand() % range + 1;
|
||||
|
||||
size = ARRAY_LEN(array);
|
||||
|
||||
|
Some files were not shown because too many files have changed in this diff Show More
Loading…
Reference in New Issue
Block a user