Skip to content

Quick Sort and Linked Lost #27

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,3 +29,7 @@
### Sagar Gurung

- Github: https://github.com/SagarGi

### Aditya Srivastava

- Github: https://github.com/abdzitter
88 changes: 88 additions & 0 deletions Data Structure/LinkedList
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
#include <assert.h>
#include <limits.h>
#include <math.h>
#include <stdbool.h>
#include <stddef.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

char* readline();

// Complete the utopianTree function below.
int utopianTree(int n)
{
int sum=1;
for(int i=1;i<=n;i++)
{
if(i%2==0)
{
sum=sum+1;
}
else
{
sum=sum*2;
}
}
return sum;
}

int main()
{
FILE* fptr = fopen(getenv("OUTPUT_PATH"), "w");

char* t_endptr;
char* t_str = readline();
int t = strtol(t_str, &t_endptr, 10);

if (t_endptr == t_str || *t_endptr != '\0') { exit(EXIT_FAILURE); }

for (int t_itr = 0; t_itr < t; t_itr++) {
char* n_endptr;
char* n_str = readline();
int n = strtol(n_str, &n_endptr, 10);

if (n_endptr == n_str || *n_endptr != '\0') { exit(EXIT_FAILURE); }

int result = utopianTree(n);

fprintf(fptr, "%d\n", result);
}

fclose(fptr);

return 0;
}

char* readline() {
size_t alloc_length = 1024;
size_t data_length = 0;
char* data = malloc(alloc_length);

while (true) {
char* cursor = data + data_length;
char* line = fgets(cursor, alloc_length - data_length, stdin);

if (!line) { break; }

data_length += strlen(cursor);

if (data_length < alloc_length - 1 || data[data_length - 1] == '\n') { break; }

size_t new_length = alloc_length << 1;
data = realloc(data, new_length);

if (!data) { break; }

alloc_length = new_length;
}

if (data[data_length - 1] == '\n') {
data[data_length - 1] = '\0';
}

data = realloc(data, data_length);

return data;
}
56 changes: 56 additions & 0 deletions Data Structure/QuickSort
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
#include <cmath>
#include <cstdio>
#include <vector>
#include <iostream>
#include <algorithm>
using namespace std;
void swap(int *a,int *b)
{
int temp=*a;
*a=*b;
*b=temp;
}
int Partition(int a[],int low,int high)
{
int i,j;
i=low;
int pivot=a[high];
for(j=low;j<high;j++)
{
if(a[j]<=pivot)
{
swap(&a[i],&a[j]);
i++;
}
}
swap(&a[i],&a[high]);
return i;
}
void Quicksort(int a[],int low,int high)
{
if(low<high)
{
int p=Partition(a,low,high);
Quicksort(a,low,p-1);
Quicksort(a,p+1,high);
}
}


int main()
{
int n;
cin >> n;
int a[n];
for(int i=0;i<n;i++)
{
cin >>a[i];
}
Quicksort(a,0,n-1);
for(int i=0;i<n;i++)
{
cout << a[i] << " ";
}
/* Enter your code here. Read input from STDIN. Print output to STDOUT */
return 0;
}