first comm in cpp
This commit is contained in:
1 parent
5968efc884
commit
9e742f0d79
22 files changed
+154
-1379
No files matched your search
@@ -1,3 +1,2 @@
|
||||
.vscode
|
||||
exe
|
||||
*.exe
|
||||
Vendored
+53
@@ -0,0 +1,53 @@
|
||||
{
|
||||
"files.associations": {
|
||||
"ostream": "cpp",
|
||||
"atomic": "cpp",
|
||||
"bit": "cpp",
|
||||
"cctype": "cpp",
|
||||
"charconv": "cpp",
|
||||
"clocale": "cpp",
|
||||
"cmath": "cpp",
|
||||
"compare": "cpp",
|
||||
"concepts": "cpp",
|
||||
"cstddef": "cpp",
|
||||
"cstdint": "cpp",
|
||||
"cstdio": "cpp",
|
||||
"cstdlib": "cpp",
|
||||
"cstring": "cpp",
|
||||
"ctime": "cpp",
|
||||
"cwchar": "cpp",
|
||||
"exception": "cpp",
|
||||
"format": "cpp",
|
||||
"initializer_list": "cpp",
|
||||
"ios": "cpp",
|
||||
"iosfwd": "cpp",
|
||||
"iostream": "cpp",
|
||||
"istream": "cpp",
|
||||
"iterator": "cpp",
|
||||
"limits": "cpp",
|
||||
"locale": "cpp",
|
||||
"memory": "cpp",
|
||||
"new": "cpp",
|
||||
"stdexcept": "cpp",
|
||||
"streambuf": "cpp",
|
||||
"string": "cpp",
|
||||
"system_error": "cpp",
|
||||
"tuple": "cpp",
|
||||
"type_traits": "cpp",
|
||||
"typeinfo": "cpp",
|
||||
"utility": "cpp",
|
||||
"xfacet": "cpp",
|
||||
"xiosbase": "cpp",
|
||||
"xlocale": "cpp",
|
||||
"xlocbuf": "cpp",
|
||||
"xlocinfo": "cpp",
|
||||
"xlocmes": "cpp",
|
||||
"xlocmon": "cpp",
|
||||
"xlocnum": "cpp",
|
||||
"xloctime": "cpp",
|
||||
"xmemory": "cpp",
|
||||
"xstring": "cpp",
|
||||
"xtr1common": "cpp",
|
||||
"xutility": "cpp"
|
||||
}
|
||||
}
|
||||
Vendored
+27
@@ -0,0 +1,27 @@
|
||||
{
|
||||
"tasks": [
|
||||
{
|
||||
"type": "cppbuild",
|
||||
"label": "C/C++: g++.exe build active file",
|
||||
"command": "C:\\msys64\\ucrt64\\bin\\g++.exe",
|
||||
"args": [
|
||||
"-fdiagnostics-color=always",
|
||||
"-g",
|
||||
"${file}",
|
||||
"-o",
|
||||
"${workspaceFolder}\\exe\\${fileBasenameNoExtension}.exe"
|
||||
// hey check this line
|
||||
],
|
||||
|
||||
"problemMatcher": [
|
||||
"$gcc"
|
||||
],
|
||||
"group": {
|
||||
"kind": "build",
|
||||
"isDefault": true
|
||||
},
|
||||
"detail": "Task generated by Debugger."
|
||||
}
|
||||
],
|
||||
"version": "2.0.0"
|
||||
}
|
||||
@@ -1,47 +0,0 @@
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
typedef struct
|
||||
{
|
||||
int real;
|
||||
int imag;
|
||||
} ldaComplex;
|
||||
|
||||
void print(ldaComplex *lda)
|
||||
{
|
||||
printf("%d + %di", lda->real, lda->imag);
|
||||
}
|
||||
// this is the best of my ability in c: returning string is a Fucking Mess
|
||||
// i would love to do this in rust
|
||||
|
||||
ldaComplex add(ldaComplex a, ldaComplex b)
|
||||
{
|
||||
return (ldaComplex){a.real + b.real, a.imag + b.imag};
|
||||
}
|
||||
ldaComplex multiply(ldaComplex a, ldaComplex b)
|
||||
{
|
||||
return (ldaComplex){a.real * b.real - a.imag * b.imag,
|
||||
a.real * b.imag + a.imag * b.real};
|
||||
}
|
||||
|
||||
int main(int argc, char const *argv[])
|
||||
{
|
||||
ldaComplex lda = {1, 2};
|
||||
ldaComplex lda2 = {5, 2};
|
||||
ldaComplex addRes = add(lda, lda2);
|
||||
ldaComplex multiRes = multiply(lda, lda2);
|
||||
|
||||
printf("(");
|
||||
print(&lda);
|
||||
printf(") + (");
|
||||
print(&lda2);
|
||||
printf(") = ");
|
||||
print(&addRes);
|
||||
printf("\n(");
|
||||
print(&lda);
|
||||
printf(") * (");
|
||||
print(&lda2);
|
||||
printf(") = ");
|
||||
print(&multiRes);
|
||||
return 0;
|
||||
}
|
||||
// ass
|
||||
@@ -0,0 +1,57 @@
|
||||
#include <iostream>
|
||||
#include <string>
|
||||
|
||||
typedef struct
|
||||
{
|
||||
int real;
|
||||
int imag;
|
||||
} ldaComplex;
|
||||
|
||||
void print(const ldaComplex *lda)
|
||||
{
|
||||
std::printf("%d + %di", lda->real, lda->imag);
|
||||
}
|
||||
|
||||
std::string to_string(const ldaComplex *lda)
|
||||
{
|
||||
return std::to_string(lda->real) + " + " + std::to_string(lda->imag) + "i";
|
||||
}
|
||||
/**
|
||||
* @brief Adds two complex numbers.
|
||||
*
|
||||
* @param[in] a The first complex number.
|
||||
* @param[in] b The second complex number.
|
||||
*
|
||||
* @return The sum of a and b.
|
||||
*/
|
||||
ldaComplex add(const ldaComplex a, const ldaComplex b)
|
||||
{
|
||||
return ldaComplex{a.real + b.real, a.imag + b.imag};
|
||||
}
|
||||
|
||||
ldaComplex multiply(const ldaComplex a, const ldaComplex b)
|
||||
{
|
||||
return ldaComplex{a.real * b.real - a.imag * b.imag,
|
||||
a.real * b.imag + a.imag * b.real};
|
||||
}
|
||||
ldaComplex operator+(const ldaComplex a, const ldaComplex b)
|
||||
{
|
||||
return add(a, b);
|
||||
}
|
||||
ldaComplex operator*(const ldaComplex a, const ldaComplex b)
|
||||
{
|
||||
return multiply(a, b);
|
||||
}
|
||||
int main(int argc, char const *argv[])
|
||||
{
|
||||
ldaComplex lda = {1, 2};
|
||||
ldaComplex lda2 = {5, 2};
|
||||
ldaComplex addRes = add(lda, lda2);
|
||||
ldaComplex addResadd = lda + lda2;
|
||||
ldaComplex multiRes = multiply(lda, lda2);
|
||||
ldaComplex multiResmul = lda * lda2;
|
||||
std::cout << to_string(&lda) + " + " + to_string(&lda2) + " = " + to_string(&addRes) << " (" << to_string(&addResadd) << ")" << std::endl;
|
||||
std::cout << to_string(&lda) + " * " + to_string(&lda2) + " = " + to_string(&multiRes) << " (" << to_string(&multiResmul) << ")" << std::endl;
|
||||
return 0;
|
||||
}
|
||||
// ass
|
||||
@@ -1,15 +1,15 @@
|
||||
#include <iostream>
|
||||
void findMax(int *max, int a)
|
||||
{
|
||||
if (a > *max)
|
||||
*max = a;
|
||||
};
|
||||
|
||||
#include <stdio.h>
|
||||
int main()
|
||||
{
|
||||
int c = 176;
|
||||
int f = 10;
|
||||
findMax(&f, c);
|
||||
printf("%d, %d", c, f);
|
||||
std::printf("%d, %d", c, f);
|
||||
return 0;
|
||||
}
|
||||
@@ -1,4 +1,4 @@
|
||||
#include <stdio.h>
|
||||
#include <iostream>
|
||||
|
||||
int log10intint(int n)
|
||||
{
|
||||
@@ -12,7 +12,8 @@ int log10intint(int n)
|
||||
if (n - a < 0)
|
||||
return i;
|
||||
}
|
||||
};
|
||||
return ~(1 << 31);
|
||||
}
|
||||
int ten_to_the(int i)
|
||||
{
|
||||
if (i < 0)
|
||||
@@ -1,7 +1,5 @@
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <stdbool.h>
|
||||
|
||||
#include <iostream>
|
||||
#include "sphenic.hpp"
|
||||
bool sphenic(int i)
|
||||
{
|
||||
if (i < 30)
|
||||
@@ -0,0 +1 @@
|
||||
bool sphenic(int i);
|
||||
-195
@@ -1,195 +0,0 @@
|
||||
#include "../common/linkedlist.h"
|
||||
#include <string.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h> // reimport!!! as
|
||||
#include <stdbool.h>
|
||||
|
||||
typedef struct CarInfo
|
||||
{
|
||||
int capacity;
|
||||
char *id; // i follow the thing
|
||||
// (0, cap]
|
||||
int reserved;
|
||||
} CarInfo;
|
||||
|
||||
typedef struct Train
|
||||
{
|
||||
dllda Cars;
|
||||
int passengers;
|
||||
} Train;
|
||||
|
||||
// rust is perfect for this let me tell you
|
||||
|
||||
CarInfo *CarInit(int cap, char *id)
|
||||
{
|
||||
CarInfo *c = (CarInfo *)calloc(1, sizeof(CarInfo));
|
||||
if (cap > 0)
|
||||
c->capacity = cap;
|
||||
c->id = strdup(id);
|
||||
return c;
|
||||
}
|
||||
|
||||
int trainLength(Train *train) { return (train->Cars).size; }
|
||||
int car_check(CarInfo *c)
|
||||
{
|
||||
if (c->reserved <= 0)
|
||||
return -1;
|
||||
else if (c->reserved > c->capacity)
|
||||
return 1;
|
||||
return 0;
|
||||
}
|
||||
int getPassengers(CarInfo *c) { return c->reserved; }
|
||||
int getCapacity(CarInfo *c) { return c->capacity; }
|
||||
|
||||
// negative passengers
|
||||
int CarModifyPassengers(CarInfo *car, int passengers)
|
||||
{
|
||||
int current = getPassengers(car);
|
||||
int capa = getCapacity(car);
|
||||
int added = current + passengers;
|
||||
if (added > capa)
|
||||
{
|
||||
car->reserved = capa;
|
||||
return added - capa; // added - newCurrent
|
||||
}
|
||||
else if (added < 0)
|
||||
{
|
||||
car->reserved = 0;
|
||||
return added;
|
||||
}
|
||||
else
|
||||
{
|
||||
car->reserved = added;
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
|
||||
int TrainModifyPassengers(Train *train, int passengers)
|
||||
{
|
||||
if (passengers == 0)
|
||||
return 0;
|
||||
int len = trainLength(train);
|
||||
dllda *Cars = &train->Cars;
|
||||
int in_or_out = passengers > 0 ? 1 : -1;
|
||||
int number = in_or_out == 1
|
||||
? 0
|
||||
: in_or_out == -1
|
||||
? len - 1
|
||||
: false;
|
||||
for (int i = number;
|
||||
in_or_out == -1
|
||||
? i >= 0
|
||||
: in_or_out == 1
|
||||
? i < len
|
||||
: false;
|
||||
i += in_or_out)
|
||||
{
|
||||
CarInfo *thisCar;
|
||||
dllda_query(Cars, i, (uintptr_t *)&thisCar);
|
||||
passengers = CarModifyPassengers(thisCar, passengers);
|
||||
// I DONT FREE cars fuckleyoos
|
||||
if (passengers == 0)
|
||||
break;
|
||||
}
|
||||
return passengers > 0 ? passengers : 0;
|
||||
}
|
||||
|
||||
int printTheCarThing(CarInfo *c)
|
||||
{
|
||||
return printf("CarInfo (id: %s): %d/%d", c->id, c->reserved, c->capacity);
|
||||
}
|
||||
|
||||
int printhere(Train *train)
|
||||
{
|
||||
dllda *new = &train->Cars;
|
||||
int sz = dllda_len(new);
|
||||
if (sz <= 0)
|
||||
return 0;
|
||||
printf("size: %d [", sz);
|
||||
uintptr_t *c = (uintptr_t *)calloc(sz, sizeof(uintptr_t));
|
||||
for (int i = sz - dllda_to_list(new, c);
|
||||
printf("(%d: ", i) &&
|
||||
printTheCarThing((CarInfo *)c[i]) &&
|
||||
printf(")") &&
|
||||
(++i < sz); // how to get value
|
||||
printf(", "))
|
||||
;
|
||||
printf("]\n");
|
||||
return 1;
|
||||
}
|
||||
|
||||
void cartraverse(Train *train)
|
||||
{
|
||||
dllda *l = &train->Cars;
|
||||
printf("\nthis is cars with size %d, start being uint %p, and end we do not know %p\n", l->size, l->start, l->end);
|
||||
printf("array repr: ");
|
||||
printhere(train) || printf("None\n");
|
||||
int leng = dllda_len(l);
|
||||
|
||||
nodeld *someNode = l->start;
|
||||
for (int i = 0; (i < leng); i++,
|
||||
(i < leng) && (printf("next node is\n") == 0)) // funnily
|
||||
{
|
||||
printf("with find:\n");
|
||||
nodeld *thisNode = adll_find(l, i);
|
||||
printf("node %p at %d, ", thisNode, i);
|
||||
printf("with ptr %p val ", thisNode->ptr);
|
||||
printTheCarThing((CarInfo *)thisNode->ptr);
|
||||
printf(", prev %p and next %p\n", thisNode->prev, thisNode->next);
|
||||
printf("with node traversal:\n");
|
||||
printf("node %p at %d, ", thisNode, i);
|
||||
printf("with ptr %p val ", someNode->ptr);
|
||||
printTheCarThing((CarInfo *)someNode->ptr);
|
||||
printf(", prev %p and next %p\n", someNode->prev, someNode->next);
|
||||
someNode = someNode->next;
|
||||
}
|
||||
printf("that is all.\n");
|
||||
}
|
||||
|
||||
int main(int argc, char const *argv[])
|
||||
{
|
||||
|
||||
Train train;
|
||||
train.passengers = 0;
|
||||
|
||||
train.Cars = *initlda(); // some fuck
|
||||
|
||||
dllda *Cars = &train.Cars;
|
||||
// cartraverse(&train);
|
||||
|
||||
CarInfo *Car1 = CarInit(55, "gay101");
|
||||
|
||||
// dllda *Cars = &train.Cars;
|
||||
dllda_push(Cars, (uintptr_t)Car1);
|
||||
|
||||
CarInfo *Car2 = CarInit(73, "top3");
|
||||
dllda_pushleft(Cars, (uintptr_t)Car2);
|
||||
// cartraverse(&train);
|
||||
|
||||
CarInfo *Car3 = CarInit(25, "r1chk1dz");
|
||||
dllda_pushleft(Cars, (uintptr_t)Car3);
|
||||
// cartraverse(&train);
|
||||
|
||||
CarInfo *Car4 = CarInit(14, "prez");
|
||||
dllda_insert(Cars, 2, (uintptr_t)Car4);
|
||||
cartraverse(&train);
|
||||
|
||||
uintptr_t *array = (uintptr_t *)calloc(dllda_len(Cars), sizeof(CarInfo));
|
||||
dllda_to_list(Cars, array);
|
||||
// printhere(&train);
|
||||
|
||||
int pass = 183;
|
||||
printf("pass in: %d\n", pass);
|
||||
printhere(&train);
|
||||
pass = TrainModifyPassengers(&train, pass);
|
||||
printf("pass left: %d\n", pass);
|
||||
// cartraverse(&train);
|
||||
printhere(&train);
|
||||
int out = -221;
|
||||
printf("pass out: %d\n", out);
|
||||
out = TrainModifyPassengers(&train, out);
|
||||
printhere(&train);
|
||||
printf("pass all out?: %d\n", out);
|
||||
printhere(&train);
|
||||
return 0;
|
||||
}
|
||||
@@ -1,62 +0,0 @@
|
||||
#include "../common/arraylist.h"
|
||||
// haha i will have to compile manually EVERY TIME HAHAHAHAHAHAHAHA
|
||||
|
||||
// gcc ./2/unboundedIntegers.c ./common/arraylist.c -o ./exe/inboundedIntegers && ./exe/inboundedIntegers
|
||||
#include <stdio.h>
|
||||
|
||||
ldaArrayList number_to_list(long long int e)
|
||||
{
|
||||
ldaArrayList c;
|
||||
ldalistinit(&c);
|
||||
if (e < 0)
|
||||
{
|
||||
ldalistpush(&c, -1);
|
||||
e = -e;
|
||||
}
|
||||
else
|
||||
ldalistpush(&c, 1);
|
||||
for (; e > 0; e /= 10)
|
||||
ldalistadd(&c, 1, (int)(e % 10));
|
||||
// SHIT IN (array rotato (it means bad))
|
||||
ldalistprint(&c);
|
||||
return c;
|
||||
}
|
||||
|
||||
void displayNumber(ldaArrayList *n)
|
||||
{
|
||||
for (int i = 0, cur; i < ldalistlen(n); i++)
|
||||
{
|
||||
if (ldalistlookup(n, i, &cur) != 0)
|
||||
return;
|
||||
if (cur == -1)
|
||||
printf("-");
|
||||
else
|
||||
printf("%d", cur);
|
||||
}
|
||||
}
|
||||
|
||||
long long int list_to_number(ldaArrayList c)
|
||||
{
|
||||
// SHIT OUT
|
||||
long long ret = 0;
|
||||
int e;
|
||||
for (long long int f = 1; ldalistpop(&c, &e) == 0; f *= 10)
|
||||
{
|
||||
if (e >= 0)
|
||||
ret = ret + e * f;
|
||||
else if (e == -1)
|
||||
ret = -ret;
|
||||
}
|
||||
return ret;
|
||||
}
|
||||
|
||||
// i wont bother with add rem stuff use the list methods directly
|
||||
|
||||
int main(int argc, char const *argv[])
|
||||
{
|
||||
ldaArrayList ef = number_to_list(-276482364786342);
|
||||
displayNumber(&ef);
|
||||
printf("\n");
|
||||
printf(" %lld ", list_to_number(ef));
|
||||
return 0;
|
||||
}
|
||||
@@ -1,4 +1,4 @@
|
||||
#include <stdio.h>
|
||||
#include <iostream>
|
||||
|
||||
int calculate_iter(int n)
|
||||
{
|
||||
@@ -1,6 +1,7 @@
|
||||
#include <stdio.h>
|
||||
#include <stdbool.h>
|
||||
|
||||
#include <iostream>
|
||||
#include <string>
|
||||
#include "../1/sphenic.hpp"
|
||||
using std::string;
|
||||
bool _sphenic(int num, int cnt, int pr)
|
||||
{
|
||||
// Base case: If we have already found 3 distinct primes, check if the number is fully divided.
|
||||
@@ -40,14 +41,14 @@ bool rsphenic(int n)
|
||||
int main()
|
||||
{
|
||||
int N = 200;
|
||||
|
||||
string str;
|
||||
for (int i = 1; i < N; i++)
|
||||
{
|
||||
// printf("%d\n", i);
|
||||
|
||||
if (rsphenic(i))
|
||||
printf("%d ", i);
|
||||
}
|
||||
|
||||
str += std::to_string(i) + " ";
|
||||
};
|
||||
std::cout << str << std::endl;
|
||||
return 0;
|
||||
}
|
||||
@@ -1,118 +0,0 @@
|
||||
// for printf
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include "arraylist.h"
|
||||
int CAPACITY = 24, ADDITIONAL = 16; // id rather just <<=1
|
||||
|
||||
// stack list
|
||||
void ldalistinit(ldaArrayList *l)
|
||||
{
|
||||
l->size = 0;
|
||||
l->capacity = CAPACITY;
|
||||
l->list = (int *)calloc(l->capacity, sizeof(int));
|
||||
}
|
||||
|
||||
int ldalistlen(ldaArrayList *l)
|
||||
{
|
||||
return l->size;
|
||||
}
|
||||
int ldalistMakeRoom(ldaArrayList *l)
|
||||
{
|
||||
if (l->size >= l->capacity)
|
||||
{
|
||||
l->capacity += ADDITIONAL;
|
||||
l->list = (int *)realloc(l->list, l->capacity * sizeof(int));
|
||||
return 1;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
int ldalistpush(ldaArrayList *l, int value)
|
||||
{
|
||||
// raw dogging
|
||||
ldalistMakeRoom(l);
|
||||
// write first then increase
|
||||
l->list[l->size++] = value;
|
||||
return 0;
|
||||
}
|
||||
int ldalistadd(ldaArrayList *l, int at, int value)
|
||||
{
|
||||
ldalistMakeRoom(l);
|
||||
int size = ldalistlen(l);
|
||||
if (at > size)
|
||||
return -1;
|
||||
else if (at == size)
|
||||
{
|
||||
return ldalistpush(l, value);
|
||||
}
|
||||
else
|
||||
{
|
||||
for (int i = size; i > at; i--)
|
||||
{
|
||||
l->list[i] = l->list[i - 1];
|
||||
};
|
||||
l->list[at] = value;
|
||||
l->size += 1;
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
|
||||
int ldalistpop(ldaArrayList *l, int *put_here)
|
||||
{
|
||||
if (ldalistlen(l) <= 0) // how would u like
|
||||
return -1;
|
||||
*put_here = l->list[--l->size];
|
||||
// decrease to then take out
|
||||
return 0;
|
||||
}
|
||||
|
||||
int ldalistremove(ldaArrayList *l, int at, int *put_here)
|
||||
{
|
||||
if (ldalistlen(l) <= 0)
|
||||
return -1;
|
||||
|
||||
int size = ldalistlen(l);
|
||||
if (at > size)
|
||||
return -1;
|
||||
else if (at == size)
|
||||
{
|
||||
return ldalistpop(l, put_here);
|
||||
}
|
||||
else
|
||||
{
|
||||
*put_here = l->list[at];
|
||||
for (int i = at; i++ > size;)
|
||||
{
|
||||
l->list[i] = l->list[i - 1];
|
||||
};
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
|
||||
int ldalistlookup(ldaArrayList *l, int at, int *copy_here)
|
||||
{
|
||||
if (at > ldalistlen(l))
|
||||
return -1;
|
||||
*copy_here = l->list[at];
|
||||
return 0;
|
||||
}
|
||||
|
||||
void ldalistprint(ldaArrayList *l)
|
||||
{
|
||||
printf("Array with size %d (capacity %d): ", l->size, l->capacity);
|
||||
printf("[");
|
||||
for (int i = 0, cur;
|
||||
ldalistlookup(l, i, &cur) == 0 &&
|
||||
printf("%d", cur) != 0 &&
|
||||
(++i < ldalistlen(l));
|
||||
printf(","))
|
||||
;
|
||||
|
||||
printf("]");
|
||||
};
|
||||
|
||||
// int main(int argc, char const *argv[])
|
||||
// {
|
||||
// /* code */
|
||||
// return 0;
|
||||
// }
|
||||
@@ -1,25 +0,0 @@
|
||||
// for printf
|
||||
// stack list
|
||||
|
||||
#ifndef COMMON_LDAARRAYLIST_H
|
||||
#define COMMON_LDAARRAYLIST_H
|
||||
|
||||
typedef struct LdaArrayList
|
||||
{
|
||||
int size;
|
||||
int capacity; // size big then capacity big 2
|
||||
int *list;
|
||||
} ldaArrayList;
|
||||
|
||||
void ldalistinit(ldaArrayList *l);
|
||||
int ldalistlen(ldaArrayList *l);
|
||||
// int ldalistcheck(ldaArrayList *l);
|
||||
int ldalistpush(ldaArrayList *l, int value);
|
||||
int ldalistadd(ldaArrayList *l, int at, int value);
|
||||
int ldalistpop(ldaArrayList *l, int *put_here);
|
||||
int ldalistremove(ldaArrayList *l, int at, int *put_here);
|
||||
|
||||
int ldalistlookup(ldaArrayList *l, int at, int *copy_here);
|
||||
|
||||
void ldalistprint(ldaArrayList *l);
|
||||
#endif
|
||||
@@ -1,451 +0,0 @@
|
||||
#include "linkedlist.h"
|
||||
#include <stdio.h>
|
||||
// im not good at c!!!!! i do not use the Potential it has
|
||||
|
||||
// BE INCREDIBLY FR WITH NODEPTR
|
||||
// am i doing it right
|
||||
|
||||
#define elif else if
|
||||
|
||||
// removehidden
|
||||
|
||||
dllda *initlda()
|
||||
{
|
||||
return (dllda *)calloc(1, sizeof(dllda));
|
||||
}
|
||||
nodeld *nodeinit(uintptr_t ptr)
|
||||
{
|
||||
nodeld *new = (nodeld *)calloc(1, sizeof(nodeld));
|
||||
// printf("%p ", new);
|
||||
new->ptr = ptr;
|
||||
return new;
|
||||
}
|
||||
// int dllda_init(dllda *l, uintptr_t firstValue)
|
||||
// {
|
||||
// l->size = 1;
|
||||
// nodeld start;
|
||||
// start.ptr = firstValue;
|
||||
// l->start = &start;
|
||||
// start.prev = NULL, start.next = NULL;
|
||||
// printf("%p,%p,%p,%d,%p,%p", l, &start, start.ptr, *(int *)start.ptr, start.prev, start.next);
|
||||
// return 0;
|
||||
// }
|
||||
|
||||
// auto size--
|
||||
int adll_remove(dllda *whatif, nodeld *node)
|
||||
{
|
||||
if (whatif->size <= 0)
|
||||
{
|
||||
whatif->size = 0;
|
||||
return -1;
|
||||
}
|
||||
if (!(node->prev == NULL || node == whatif->start) &&
|
||||
!(node->next == NULL || node == whatif->end)) // not a not b
|
||||
{
|
||||
// left of right now is left
|
||||
node->next->prev = node->prev;
|
||||
// right of left now is right
|
||||
node->prev->next = node->next;
|
||||
}
|
||||
// no left.
|
||||
else if (node->prev == NULL || node == whatif->start) // a
|
||||
{
|
||||
// start = right
|
||||
whatif->start = node->next;
|
||||
|
||||
if (whatif->start == whatif->end)
|
||||
whatif->end = NULL; // ass font
|
||||
|
||||
// left of right now is null
|
||||
if (whatif->start != NULL)
|
||||
whatif->start->prev = NULL;
|
||||
}
|
||||
// no right!
|
||||
else if (node->next == NULL || node == whatif->end) // b
|
||||
{
|
||||
// end is now left
|
||||
whatif->end = node->prev;
|
||||
// right of left now is null
|
||||
if (whatif->end != NULL)
|
||||
whatif->end->next = NULL;
|
||||
if (whatif->start == whatif->end)
|
||||
whatif->end = NULL; // i do hate life do i
|
||||
}
|
||||
|
||||
whatif->size--;
|
||||
free(node);
|
||||
// free(node);
|
||||
return 0;
|
||||
|
||||
// how do i either know the position or know the
|
||||
}
|
||||
|
||||
int structure_fixups(dllda *l)
|
||||
{
|
||||
nodeld *start = l->start, *end = l->end;
|
||||
if (!(start || end))
|
||||
{
|
||||
printf("\nnothing\n");
|
||||
l->size = 0;
|
||||
return 0;
|
||||
}
|
||||
else if (start == NULL)
|
||||
{
|
||||
start = end;
|
||||
int count = 1;
|
||||
while (start->prev != NULL)
|
||||
{
|
||||
start = start->prev;
|
||||
count++;
|
||||
}
|
||||
l->start = start;
|
||||
if (count == 1)
|
||||
l->end = l->start->next;
|
||||
l->size = count;
|
||||
return 2;
|
||||
}
|
||||
else if (end == NULL)
|
||||
{
|
||||
end = start;
|
||||
int count = 1;
|
||||
while (end->next != NULL)
|
||||
{
|
||||
end = end->next;
|
||||
count++;
|
||||
}
|
||||
if (count > 1)
|
||||
l->end = end;
|
||||
l->size = count;
|
||||
return 1;
|
||||
}
|
||||
else
|
||||
return 3;
|
||||
{
|
||||
int cfl = 1, cfr = 1;
|
||||
while (end->prev || start->next)
|
||||
{
|
||||
if (end->prev && start->next == 0)
|
||||
{
|
||||
printf("%p, %p", end->prev, start->next);
|
||||
printf("%d, %d", cfl, cfr);
|
||||
};
|
||||
|
||||
{
|
||||
end = end->prev;
|
||||
cfr++;
|
||||
}
|
||||
{
|
||||
start = start->next;
|
||||
cfl++;
|
||||
}
|
||||
}
|
||||
if (cfl == cfr)
|
||||
l->size = cfl;
|
||||
};
|
||||
}
|
||||
|
||||
void dllda_pushleft(dllda *c, uintptr_t ptr)
|
||||
{
|
||||
nodeld *new = nodeinit(ptr);
|
||||
structure_fixups(c);
|
||||
|
||||
if (c->start != NULL)
|
||||
{
|
||||
new->next = c->start;
|
||||
new->next->prev = new;
|
||||
}
|
||||
c->start = new;
|
||||
c->size++;
|
||||
}
|
||||
void dllda_push(dllda *c, uintptr_t ptr)
|
||||
{
|
||||
// printf("%p %p \n", &c, c);
|
||||
nodeld *new = nodeinit(ptr);
|
||||
// printf("%p ", new);
|
||||
structure_fixups(c);
|
||||
if (c->end || c->start)
|
||||
{
|
||||
new->prev = c->end ? c->end : c->start;
|
||||
new->prev->next = new;
|
||||
c->end = new;
|
||||
}
|
||||
else
|
||||
c->start = new;
|
||||
|
||||
c->size++;
|
||||
}
|
||||
|
||||
int dllda_len(dllda *c) { return c->size; }
|
||||
|
||||
nodeld *adll_find(dllda *l, int i)
|
||||
{
|
||||
int len = dllda_len(l);
|
||||
if (i < 0 || i >= len)
|
||||
return NULL;
|
||||
int attack_from = 1;
|
||||
nodeld *curnode = l->start;
|
||||
int from = 0;
|
||||
|
||||
if (i > len / 2)
|
||||
{
|
||||
attack_from = -1;
|
||||
curnode = l->end;
|
||||
from = len - 1;
|
||||
}
|
||||
|
||||
// start is 0 end is len l - 1 aka -1
|
||||
for (int cur = from; cur != i; cur += attack_from)
|
||||
{
|
||||
if (attack_from == 1)
|
||||
curnode = curnode->next;
|
||||
else if (attack_from == -1)
|
||||
curnode = curnode->prev;
|
||||
}
|
||||
return curnode;
|
||||
}
|
||||
int dllda_rem(dllda *l, int pos, uintptr_t *puthere)
|
||||
{
|
||||
nodeld *c = adll_find(l, pos);
|
||||
*puthere = (uintptr_t)c->ptr;
|
||||
adll_remove(l, c);
|
||||
return 0;
|
||||
}
|
||||
|
||||
int dllda_query(dllda *l, int i, uintptr_t *copy_here)
|
||||
{
|
||||
nodeld *g = adll_find(l, i);
|
||||
if (g == NULL)
|
||||
{
|
||||
if (dllda_len(l) == 0)
|
||||
return -1;
|
||||
else
|
||||
return -2;
|
||||
};
|
||||
// printf("%p, %p", g, &g);
|
||||
*copy_here = g->ptr;
|
||||
return 0;
|
||||
}
|
||||
int dllda_popleft(dllda *l, uintptr_t *puthere)
|
||||
{
|
||||
nodeld *startMayBeNull = adll_find(l, 0);
|
||||
if (startMayBeNull == NULL)
|
||||
{ // start now is No Longer Null
|
||||
if (dllda_len(l) == 0)
|
||||
return -1;
|
||||
else
|
||||
return -2;
|
||||
};
|
||||
*puthere = startMayBeNull->ptr;
|
||||
adll_remove(l, startMayBeNull);
|
||||
if (dllda_len(l) == 1)
|
||||
{
|
||||
if (l->start == NULL)
|
||||
;
|
||||
};
|
||||
return 0;
|
||||
}
|
||||
|
||||
int dllda_pop(dllda *l, uintptr_t *puthere)
|
||||
{
|
||||
nodeld *endMayBeNull = adll_find(l, dllda_len(l) - 1);
|
||||
if (endMayBeNull == NULL)
|
||||
{
|
||||
int len = dllda_len(l);
|
||||
if (len == 1)
|
||||
return dllda_popleft(l, puthere);
|
||||
elif (len == 0) return -1;
|
||||
else return -2;
|
||||
};
|
||||
*puthere = endMayBeNull->ptr;
|
||||
adll_remove(l, endMayBeNull);
|
||||
return 0;
|
||||
}
|
||||
// no add to size, also bugs when edges (i mean this is a interceptor)
|
||||
int adll_insert(nodeld *tobeplaced, uintptr_t ptr)
|
||||
{
|
||||
nodeld *next = tobeplaced;
|
||||
nodeld *prev = next->prev;
|
||||
nodeld *new = nodeinit(ptr);
|
||||
new->prev = prev;
|
||||
new->next = next;
|
||||
if (prev->next != next || next->prev != prev)
|
||||
{
|
||||
free(new);
|
||||
return -1;
|
||||
}
|
||||
prev->next = new;
|
||||
next->prev = new;
|
||||
return 0;
|
||||
}
|
||||
|
||||
int dllda_insert(dllda *l, int pos, uintptr_t ptr)
|
||||
{
|
||||
int len = dllda_len(l);
|
||||
if (0 < pos && pos < len)
|
||||
{
|
||||
nodeld *replaced = adll_find(l, pos);
|
||||
if (replaced == NULL)
|
||||
return -1;
|
||||
if (adll_insert(replaced, ptr) != 0)
|
||||
return -1;
|
||||
l->size++;
|
||||
return 0;
|
||||
}
|
||||
else if (pos == len) // funny you can do that
|
||||
dllda_push(l, ptr);
|
||||
else if (pos == 0)
|
||||
dllda_pushleft(l, ptr);
|
||||
else
|
||||
return -1;
|
||||
return 0;
|
||||
}
|
||||
|
||||
int dllda_peek(dllda *l, uintptr_t *copy_here)
|
||||
{
|
||||
return dllda_query(l, dllda_len(l) - 1, copy_here);
|
||||
}
|
||||
int dllda_peekleft(dllda *l, uintptr_t *copy_here)
|
||||
{
|
||||
return dllda_query(l, 0, copy_here);
|
||||
}
|
||||
|
||||
void printItOut(dllda *new)
|
||||
{
|
||||
int sz = dllda_len(new);
|
||||
printf("size: %d [", sz);
|
||||
uintptr_t *c = (uintptr_t *)calloc(sz, sizeof(uintptr_t));
|
||||
for (int i = sz - dllda_to_list(new, c);
|
||||
(i < sz); // how to get value
|
||||
i++,
|
||||
(i < sz) && printf(", "))
|
||||
printf("(%d: %d)", i, *(int *)c[i]);
|
||||
printf("]\n");
|
||||
}
|
||||
void traverse(dllda *l)
|
||||
{
|
||||
printf("\nthis is l with size %d, start being uint %p, and end we do not know %p\n", l->size, l->start, l->end);
|
||||
printf("array repr: ");
|
||||
printItOut(l);
|
||||
int leng = dllda_len(l);
|
||||
|
||||
nodeld *someNode = l->start;
|
||||
for (int i = 0; (i < leng); i++, // funnily
|
||||
(i < leng) && (printf("somenode next now.\n") == 0))
|
||||
{
|
||||
printf("with find:\n");
|
||||
nodeld *thisNode = adll_find(l, i);
|
||||
printf("node %p at %d, ", thisNode, i);
|
||||
printf("with ptr %p val %d, ", thisNode->ptr, *(int *)thisNode->ptr);
|
||||
printf("prev %p and next %p\n", thisNode->prev, thisNode->next);
|
||||
printf("with node traversal:\n");
|
||||
printf("node? at %d, ", i);
|
||||
printf("with ptr %p val %d, ", someNode->ptr, *(int *)someNode->ptr);
|
||||
printf("prev %p and next %p\n", someNode->prev, someNode->next);
|
||||
someNode = someNode->next;
|
||||
}
|
||||
printf("that is all.\n");
|
||||
}
|
||||
|
||||
// return sizes
|
||||
int dllda_to_list(dllda *l, uintptr_t *arr)
|
||||
{
|
||||
int len = dllda_len(l);
|
||||
nodeld *node = l->start;
|
||||
for (int i = 0; i < len; i++)
|
||||
{
|
||||
arr[i] = node->ptr;
|
||||
node = node->next;
|
||||
}
|
||||
return len;
|
||||
}
|
||||
|
||||
int dllda_into_list(dllda *l, uintptr_t *arr)
|
||||
{
|
||||
int len = dllda_len(l);
|
||||
for (int i = 0; i < len; i++)
|
||||
{
|
||||
uintptr_t *t = &(arr[i]);
|
||||
dllda_popleft(l, t);
|
||||
// printf("%d ", *(int*) *t);
|
||||
}
|
||||
return len;
|
||||
}
|
||||
|
||||
int dllda_from_list(dllda *l, uintptr_t *arr, int size)
|
||||
{
|
||||
for (int i = 0; i < size; i++)
|
||||
|
||||
// printf("%p %d ", arr[i], *(int *)arr[i]);
|
||||
dllda_push(l, arr[i]);
|
||||
// printf("%d ", *(int*) *t);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
int main(int argc, char const *argv[])
|
||||
{
|
||||
dllda *new = initlda();
|
||||
int dsa = 17;
|
||||
dllda_push(new, (uintptr_t)&dsa);
|
||||
// traverse(new);
|
||||
printItOut(new);
|
||||
int item4 = -2;
|
||||
dllda_pushleft(new, (uintptr_t)&item4);
|
||||
printItOut(new);
|
||||
// traverse(new);
|
||||
int item1 = 1;
|
||||
dllda_pushleft(new, (uintptr_t)&item1);
|
||||
// traverse(new);
|
||||
printItOut(new);
|
||||
int item5 = 9;
|
||||
dllda_insert(new, 2, (uintptr_t)&item5);
|
||||
printItOut(new);
|
||||
// traverse(new);
|
||||
|
||||
int item2 = 4;
|
||||
dllda_push(new, (uintptr_t)&item2);
|
||||
// traverse(new);
|
||||
int item3 = 13;
|
||||
dllda_push(new, (uintptr_t)&item3);
|
||||
// traverse(new);
|
||||
int item6 = 25;
|
||||
dllda_insert(new, 0, (uintptr_t)&item6);
|
||||
// printItOut(new);
|
||||
// traverse(new);
|
||||
uintptr_t this1, this2, this3;
|
||||
int code;
|
||||
code = dllda_rem(new, 3, &this1);
|
||||
printf(" %d ", *(int *)this1);
|
||||
// printItOut(new);
|
||||
code = dllda_popleft(new, &this2);
|
||||
printf(" %d ", *(int *)this2);
|
||||
// printItOut(new);
|
||||
code = dllda_pop(new, &this3);
|
||||
printf(" %d ", *(int *)this3);
|
||||
// printItOut(new);
|
||||
// traverse(new);
|
||||
printf("\n");
|
||||
int item7 = 11;
|
||||
dllda_insert(new, 2, (uintptr_t)&item7);
|
||||
int item8 = 42;
|
||||
dllda_insert(new, 1, (uintptr_t)&item8);
|
||||
int item9 = 16;
|
||||
dllda_insert(new, 3, (uintptr_t)&item9);
|
||||
dllda_push(new, this1);
|
||||
dllda_push(new, this2);
|
||||
dllda_pushleft(new, this3);
|
||||
printItOut(new);
|
||||
printf("\n");
|
||||
int len = dllda_len(new);
|
||||
uintptr_t *list = calloc(len, sizeof(uintptr_t));
|
||||
dllda_into_list(new, list);
|
||||
for (int i = 0; i < len; i++)
|
||||
printf("%d ", *(int *)list[i]);
|
||||
printf("\n");
|
||||
printItOut(new);
|
||||
printf("a\n");
|
||||
dllda_from_list(new, list, len);
|
||||
printItOut(new);
|
||||
|
||||
return 0;
|
||||
}
|
||||
@@ -1,45 +0,0 @@
|
||||
#ifndef LDA_LINKEDLIST_H
|
||||
#define LDA_LINKEDLIST_H
|
||||
// free with: the good of the c
|
||||
#include <stdlib.h>
|
||||
typedef struct Node
|
||||
{
|
||||
uintptr_t ptr; // remember to cast!
|
||||
struct Node *prev;
|
||||
struct Node *next;
|
||||
} nodeld;
|
||||
typedef struct DoublyLinkedList
|
||||
{
|
||||
int size;
|
||||
nodeld *start;
|
||||
nodeld *end;
|
||||
} dllda;
|
||||
|
||||
dllda *initlda();
|
||||
nodeld *nodeinit(uintptr_t ptr);
|
||||
int structure_fixups(dllda *l);
|
||||
|
||||
void dllda_pushleft(dllda *c, uintptr_t ptr);
|
||||
void dllda_push(dllda *c, uintptr_t ptr);
|
||||
int dllda_insert(dllda *l, int pos, uintptr_t ptr);
|
||||
|
||||
// semi hidden node method
|
||||
nodeld *adll_find(dllda *l, int i);
|
||||
int dllda_len(dllda *c);
|
||||
int dllda_query(dllda *l, int i, uintptr_t *copy_here);
|
||||
|
||||
int dllda_peekleft(dllda *l, uintptr_t *copy_here);
|
||||
int dllda_peek(dllda *l, uintptr_t *copy_here);
|
||||
|
||||
// int adll_insert(nodeld *tobeplaced, uintptr_t ptr);
|
||||
// int adll_remove(dllda *whatif, nodeld *node);
|
||||
|
||||
int dllda_rem(dllda *l, int pos, uintptr_t *puthere);
|
||||
int dllda_popleft(dllda *l, uintptr_t *puthere);
|
||||
int dllda_pop(dllda *l, uintptr_t *puthere);
|
||||
|
||||
// void traverse(dllda *l);
|
||||
// void printItOut(dllda *new);
|
||||
|
||||
int dllda_to_list(dllda *l, uintptr_t *arr);
|
||||
#endif
|
||||
@@ -1,51 +0,0 @@
|
||||
// feed in a pointer to a function that takes 2 and returns (just like the )
|
||||
#include <stdlib.h>
|
||||
#include <stdio.h>
|
||||
#include <stdbool.h>
|
||||
#include <math.h>
|
||||
|
||||
int cmpfunc(const void *a, const void *b)
|
||||
{
|
||||
const int *A = a, *B = b;
|
||||
return (*A > *B) - (*A < *B);
|
||||
}
|
||||
int _centeredCube(int num) {
|
||||
int low = -num, high = num, mid, result = 0;
|
||||
while (low <= high) {
|
||||
mid = low + (high - low) / 2;
|
||||
|
||||
long long thisSum = (long long) pow(mid, 3) + (long long) pow(mid + 1, 3); // Avoid overflow
|
||||
|
||||
if (thisSum == (long long) num)
|
||||
return mid;
|
||||
else if (thisSum < num) {
|
||||
result = mid; // Store the best approximation so far
|
||||
low = mid + 1;
|
||||
} else {
|
||||
high = mid - 1;
|
||||
}
|
||||
}
|
||||
return result; // Return the closest guess when exact match is not found
|
||||
};
|
||||
bool centerCube(int num) {
|
||||
int n = _centeredCube(num);
|
||||
long long thisSum = (long long) pow(n, 3) + (long long) pow(n + 1, 3);
|
||||
printf("n = %d, thisSum = %lld, num = %d\n", n, thisSum, num);
|
||||
return thisSum == (long long) num;
|
||||
}
|
||||
|
||||
int main(int argc, char const *argv[])
|
||||
{
|
||||
int a[13] = {1, 2, 3, 7, 3, 9, 11, 13, 42, -1, 4, 52, 10};
|
||||
qsort(a, 13, sizeof(int), cmpfunc);
|
||||
for (int i = 0; i < 13; i++)
|
||||
printf("%d ", a[i]);
|
||||
printf("\n");
|
||||
int at = 5;
|
||||
int *here = (int *)bsearch(&at, a, 13, sizeof(int), cmpfunc);
|
||||
if (here == NULL)
|
||||
printf("no %d ", at);
|
||||
else
|
||||
printf("is %d ", *here);
|
||||
return 0;
|
||||
}
|
||||
-235
@@ -1,235 +0,0 @@
|
||||
#include <stdio.h>
|
||||
|
||||
// Function to perform Cocktail Shaker Sort (Bidirectional Bubble Sort)
|
||||
void cocktailShakerSort(int arr[], int n)
|
||||
{
|
||||
int swapped = 1; // To track if any swaps were made
|
||||
int start = 0; // Starting index
|
||||
int end = n - 1; // Ending index
|
||||
|
||||
while (swapped)
|
||||
{
|
||||
swapped = 0;
|
||||
|
||||
// Forward pass: left to right
|
||||
for (int i = start; i < end; ++i)
|
||||
{
|
||||
if (arr[i] > arr[i + 1])
|
||||
{
|
||||
// Swap if the current element is greater than the next
|
||||
int temp = arr[i];
|
||||
arr[i] = arr[i + 1];
|
||||
arr[i + 1] = temp;
|
||||
swapped = 1;
|
||||
}
|
||||
}
|
||||
|
||||
// If no swaps occurred, the array is sorted
|
||||
if (!swapped)
|
||||
break;
|
||||
|
||||
// Reduce the end point since the last element is sorted
|
||||
--end;
|
||||
|
||||
// Backward pass: right to left
|
||||
for (int i = end - 1; i >= start; --i)
|
||||
{
|
||||
if (arr[i] > arr[i + 1])
|
||||
{
|
||||
// Swap if the current element is greater than the next
|
||||
int temp = arr[i];
|
||||
arr[i] = arr[i + 1];
|
||||
arr[i + 1] = temp;
|
||||
swapped = 1;
|
||||
}
|
||||
}
|
||||
|
||||
// Increment the start point since the first element is sorted
|
||||
++start;
|
||||
}
|
||||
}
|
||||
|
||||
// Helper function to print the array
|
||||
void printArray(int arr[], int n)
|
||||
{
|
||||
for (int i = 0; i < n; i++)
|
||||
{
|
||||
printf("%d ", arr[i]);
|
||||
}
|
||||
printf("\n");
|
||||
}
|
||||
|
||||
|
||||
// Swap function to swap two elements
|
||||
void swap(int *a, int *b) {
|
||||
int temp = *a;
|
||||
*a = *b;
|
||||
*b = temp;
|
||||
}
|
||||
|
||||
// Selection Sort function
|
||||
void selectionSort(int arr[], int n) {
|
||||
for (int i = 0; i < n - 1; i++) {
|
||||
int minIndex = i; // Assume the current element is the minimum
|
||||
|
||||
// Find the index of the minimum element in the unsorted portion
|
||||
for (int j = i + 1; j < n; j++) {
|
||||
if (arr[j] < arr[minIndex]) {
|
||||
minIndex = j;
|
||||
}
|
||||
}
|
||||
|
||||
// Swap the minimum element with the first element of the unsorted portion
|
||||
if (minIndex != i) {
|
||||
swap(&arr[i], &arr[minIndex]);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
int findMinIndex(int arr[], int start, int n) {
|
||||
int minIndex = start;
|
||||
for (int i = start + 1; i < n; i++) {
|
||||
if (arr[i] < arr[minIndex]) {
|
||||
minIndex = i;
|
||||
}
|
||||
}
|
||||
return minIndex;
|
||||
}
|
||||
|
||||
// Recursive Selection Sort function
|
||||
void recursiveSelectionSort(int arr[], int start, int n) {
|
||||
if (start >= n - 1) {
|
||||
return; // Base case: when there's only one element left, array is sorted
|
||||
}
|
||||
|
||||
// Find the minimum element in the remaining unsorted array
|
||||
int minIndex = findMinIndex(arr, start, n);
|
||||
|
||||
// Swap it with the first element of the unsorted portion
|
||||
if (minIndex != start) {
|
||||
swap(&arr[start], &arr[minIndex]);
|
||||
}
|
||||
|
||||
// Recur for the remaining unsorted portion
|
||||
recursiveSelectionSort(arr, start + 1, n);
|
||||
}
|
||||
|
||||
|
||||
void insertionSort(int arr[], int n) {
|
||||
for (int i = 1; i < n; i++) {
|
||||
for (int j = i; j > 0 && arr[j] < arr[j - 1]; j--) {
|
||||
// Swap if the current element is smaller than the previous one
|
||||
swap(&arr[j], &arr[j - 1]);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Helper function to insert element in the sorted portion recursively
|
||||
void insertElement(int arr[], int n) {
|
||||
if (n <= 0) {
|
||||
return;
|
||||
}
|
||||
|
||||
insertElement(arr, n - 1); // Sort the first n-1 elements
|
||||
|
||||
// Now insert the nth element into the sorted portion
|
||||
for (int j = n; j > 0 && arr[j] < arr[j - 1]; j--) {
|
||||
swap(&arr[j], &arr[j - 1]);
|
||||
}
|
||||
}
|
||||
|
||||
// Recursive Insertion Sort function
|
||||
void recursiveInsertionSort(int arr[], int n) {
|
||||
if (n <= 1) {
|
||||
return; // Base case: when the array has 1 or fewer elements, it's sorted
|
||||
}
|
||||
|
||||
recursiveInsertionSort(arr, n - 1); // Sort the first n-1 elements
|
||||
|
||||
insertElement(arr, n - 1); // Insert the nth element in the correct position
|
||||
}
|
||||
|
||||
// Merge two sorted subarrays arr[l..m] and arr[m+1..r]
|
||||
void merge(int arr[], int l, int m, int r)
|
||||
{
|
||||
int n1 = m - l + 1; // Size of the left subarray
|
||||
int n2 = r - m; // Size of the right subarray
|
||||
|
||||
// Temporary arrays to hold the values
|
||||
int *left = (int *)malloc(n1 * sizeof(int));
|
||||
int *right = (int *)malloc(n2 * sizeof(int));
|
||||
|
||||
// Copy data to temp arrays
|
||||
for (int i = 0; i < n1; i++)
|
||||
left[i] = arr[l + i];
|
||||
for (int i = 0; i < n2; i++)
|
||||
right[i] = arr[m + 1 + i];
|
||||
|
||||
// Merge the temp arrays back into arr[l..r]
|
||||
int i = 0, j = 0, k = l;
|
||||
while (i < n1 && j < n2)
|
||||
{
|
||||
if (left[i] <= right[j])
|
||||
{
|
||||
arr[k] = left[i];
|
||||
i++;
|
||||
}
|
||||
else
|
||||
{
|
||||
arr[k] = right[j];
|
||||
j++;
|
||||
}
|
||||
k++;
|
||||
}
|
||||
|
||||
// Copy the remaining elements of left[], if any
|
||||
while (i < n1)
|
||||
{
|
||||
arr[k] = left[i];
|
||||
i++;
|
||||
k++;
|
||||
}
|
||||
|
||||
// Copy the remaining elements of right[], if any
|
||||
while (j < n2)
|
||||
{
|
||||
arr[k] = right[j];
|
||||
j++;
|
||||
k++;
|
||||
}
|
||||
free(left);
|
||||
free(right);
|
||||
}
|
||||
|
||||
// Function to split the array and call merge on the halves
|
||||
void mergeSort(int arr[], int l, int r)
|
||||
{
|
||||
if (l < r)
|
||||
{
|
||||
int m = l + (r - l) / 2; // Find the middle point
|
||||
|
||||
// Recursively sort the first and second halves
|
||||
mergeSort(arr, l, m);
|
||||
mergeSort(arr, m + 1, r);
|
||||
|
||||
// Merge the sorted halves
|
||||
merge(arr, l, m, r);
|
||||
}
|
||||
}
|
||||
|
||||
int main()
|
||||
{
|
||||
int arr[] = {5, 1, 4, 2, 8, 0, 3, 12, 11, 64, 25, 12, 22, 11, 13, 5, 6, 7};
|
||||
int n = sizeof(arr) / sizeof(arr[0]);
|
||||
|
||||
printf("Original array: ");
|
||||
printArray(arr, n);
|
||||
// cocktailShakerSort(arr, n);
|
||||
// mergeSort(arr, 0, n - 1);
|
||||
|
||||
printf("Sorted array: ");
|
||||
printArray(arr, n);
|
||||
|
||||
return 0;
|
||||
}
|
||||
@@ -1,29 +0,0 @@
|
||||
#include <stdio.h>
|
||||
int arrPrinter(int *l, int sz)
|
||||
{
|
||||
printf("array (size %d): [", sz);
|
||||
for (
|
||||
int i = 0;
|
||||
(printf("%d", l[i]) != 0) &&
|
||||
(++i < sz);
|
||||
printf(", "))
|
||||
;
|
||||
printf("]\n");
|
||||
return 0;
|
||||
}
|
||||
|
||||
int main(int argc, char const *argv[])
|
||||
{
|
||||
int l[8] = {1, 2, 4, 6, 8, 11, 42, 76};
|
||||
printf("array l = [");
|
||||
for (
|
||||
int i = 0;
|
||||
(printf("%d", l[i]) != 0) &&
|
||||
(++i < 8);
|
||||
printf(", "))
|
||||
;
|
||||
printf("]\n");
|
||||
|
||||
arrPrinter(l, 8);
|
||||
return 0;
|
||||
}
|
||||
-101
@@ -1,101 +0,0 @@
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
typedef struct {
|
||||
int *array; // Dynamic array to store heap elements
|
||||
int size; // Current size of the heap
|
||||
int capacity; // Maximum capacity of the heap
|
||||
} MaxHeap;
|
||||
|
||||
// Function to create a max heap
|
||||
MaxHeap* createMaxHeap(int capacity) {
|
||||
MaxHeap *heap = (MaxHeap *)malloc(sizeof(MaxHeap));
|
||||
heap->capacity = capacity;
|
||||
heap->size = 0;
|
||||
heap->array = (int *)malloc(capacity * sizeof(int));
|
||||
return heap;
|
||||
}
|
||||
|
||||
// Function to swap two elements
|
||||
void swap(int *x, int *y) {
|
||||
int temp = *x;
|
||||
*x = *y;
|
||||
*y = temp;
|
||||
}
|
||||
|
||||
// Function to heapify a subtree rooted with node i
|
||||
void heapify(MaxHeap *heap, int i) {
|
||||
int largest = i; // Initialize largest as root
|
||||
int left = 2 * i + 1; // left child index
|
||||
int right = 2 * i + 2; // right child index
|
||||
|
||||
// If left child is larger than root
|
||||
if (left < heap->size && heap->array[left] > heap->array[largest]) {
|
||||
largest = left;
|
||||
}
|
||||
|
||||
// If right child is larger than largest so far
|
||||
if (right < heap->size && heap->array[right] > heap->array[largest]) {
|
||||
largest = right;
|
||||
}
|
||||
|
||||
// If largest is not root, swap and continue heapifying
|
||||
if (largest != i) {
|
||||
swap(&heap->array[i], &heap->array[largest]);
|
||||
heapify(heap, largest);
|
||||
}
|
||||
}
|
||||
|
||||
// Function to insert a new element to the heap
|
||||
void insert(MaxHeap *heap, int key) {
|
||||
if (heap->size == heap->capacity) {
|
||||
printf("Heap is full, cannot insert %d\n", key);
|
||||
return;
|
||||
}
|
||||
|
||||
// First insert the new key at the end
|
||||
heap->array[heap->size] = key;
|
||||
heap->size++;
|
||||
|
||||
// Fix the max heap property if it is violated
|
||||
for (int i = heap->size / 2 - 1; i >= 0; i--) {
|
||||
heapify(heap, i);
|
||||
}
|
||||
}
|
||||
|
||||
// Function to display the max heap
|
||||
void displayHeap(MaxHeap *heap) {
|
||||
printf("Max Heap: ");
|
||||
for (int i = 0; i < heap->size; i++) {
|
||||
printf("%d ", heap->array[i]);
|
||||
}
|
||||
printf("\n");
|
||||
}
|
||||
|
||||
// Main function
|
||||
int main() {
|
||||
int capacity;
|
||||
printf("Enter the maximum capacity of the heap: ");
|
||||
scanf("%d", &capacity);
|
||||
|
||||
MaxHeap *heap = createMaxHeap(capacity);
|
||||
|
||||
int numElements;
|
||||
printf("Enter the number of elements to insert into the heap: ");
|
||||
scanf("%d", &numElements);
|
||||
|
||||
for (int i = 0; i < numElements; i++) {
|
||||
int value;
|
||||
printf("Enter value %d: ", i + 1);
|
||||
scanf("%d", &value);
|
||||
insert(heap, value);
|
||||
}
|
||||
|
||||
displayHeap(heap);
|
||||
|
||||
// Free allocated memory
|
||||
free(heap->array);
|
||||
free(heap);
|
||||
|
||||
return 0;
|
||||
}
|
||||
Reference in new issue
Block a user