Git Product home page Git Product logo

algorithms's Introduction

I am a begginer, currently program in c.

algorithms's People

Contributors

maskofdevil avatar mohamkz avatar mridul1703 avatar privanom avatar zag8 avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar

algorithms's Issues

Add O(n log n) sorting algorithms

Is your feature request related to a problem? Please describe.
Not related to any problem, just a new addition.

Describe the solution you'd like
I would like to add implementations of sorting algorithms with improved time complexity such as merge sort and quicksort.

Describe alternatives you've considered
N/A

Additional context
N/A

Invalid algorithm

The bubble sort algorithm at bubblesort.c seems to be specifically tailored to only work on arrays with the length of 10. If the array's size is less than 10, this causes a segmentation fault, and if it's higher than 10, the array may not be fully sorted. Additionally the arrays are chars and not integers, not sure if this was intended or not, but this array will be sorted in the terms of the ASCII key structure and not numbers.

A sorting algorithm has to be able to work with arrays of variable size, learn how and when to use for & while loops, and other features C provides to achieve this.
Good luck.

6 bugs in bubblesort.c

(and no, this code was not made by me)

main.c:62:19: error: expected ')' before numeric constant
62 | bubble_sort(yarr, 10);
| ^~~
| )
main.c:65:2: error: expected identifier or '(' before 'for'
65 | for (int i = 0; i < 10; i++)
| ^~~
main.c:65:20: error: expected '=', ',', ';', 'asm' or 'attribute' before '<' token
65 | for (int i = 0; i < 10; i++)
| ^
main.c:65:27: error: expected '=', ',', ';', 'asm' or 'attribute' before '++' token
65 | for (int i = 0; i < 10; i++)
| ^~
main.c:68:2: error: expected identifier or '(' before 'return'
68 | return 0;
| ^~~~~~
main.c:69:1: error: expected identifier or '(' before '}' token
69 | }
| ^

Incorrect implementation of Merge Sort

Describe the bug
mergesort.c will not perform a valid sort upon the given array yarray[].
First of all, the length of the array size_ is not calculated properly, it should rather be done like this:

size_t len = sizeof(yarray) / sizeof(int);

In addition, the current function mergesort() is not a working implementation of the merge sort algorithm and does not produce a sorted array. Generally, merge sort is implemented using a function merge(), which merges two sorted subarrays and merge_sort(), which performs the sorting using recursive calls.

To Reproduce
Steps to reproduce the behavior:
Compile mergesort.c using Make or CMake

Expected behavior
The program should create an array and get its size, call the mergesort() function and print the sorted array.

Screenshots
N/A

Desktop (please complete the following information):

  • OS: Windows
  • Browser: N/A
  • Version: N/A

Smartphone (please complete the following information):
N/A

Additional context
Issues similar to this can be avoided by adding test functions/source files to the project along with a CMake and GitHub actions for CI.

Several issues in bogosort, linearsearch.

I will detail some of the issues that exist in bogosort and linearsearch.

bogosort.c:
Will not compile due to error on line 6, but has several other issues that need addressing.
int bogosort(yarr){} is missing a type specifier for yarr so defaults to int yarr, but you are passing it a pointer int * yarr. The actual implementation of bogosort does not make much sense either, as you do not actually sort any of the values in the array, nor do any checks to see if the array has been sorted.
for(i1 = 1; i < sizeof(yarr); i++) { static; } here it seems like you want to make i have the same size of the array but later on you set it to 0 anyway. Also using the 'static' specifier here makes no sense. I don't think sizeof(yarr) does what you think it does. You cant get the size of an array in C with just sizeof(array). It is better to either pass the length of the array with the array, e.g in a struct, or use something like sizeof(array)/sizeof(array[0]).

for(int i = 0; i < sizeof(yarr); i++)
{
    //I couldn't do yarr[i] or yarr[i1], so i did math
        do
        {
        int random_shuffle = rand() % yarr-yarr+i + yarr-yarr+i1;
        } while(yarr-yarr+i < yarr-yarr+i1);
} 

Again sizeof(yarr). Also, this yarr-yarr+i + yarr-yarr+i1 is simply the same as writing i + i1, which i dont think you intended. Besides, if you had passed a pointer as intended then you would be able to access the values in the array using operator []. Ignoring the sizeof issues, this loop would only run for one iteration due to the condition in the while loop. There are some other small problems but you should be able to sort them out. I recommend compiling with -Wall to see all warnings, and -Werror if you want to be strict with your code, as -Werror will not let you compile if there are warnings.

linearsearch.c
Again using sizeof array wrong. Also using int to hold char values. Finally, the actual implementation will not work as intended as C neither supports comparing whole strings using just the == operator or doing something like int *yarr = {"Shluck much", "much Shluck"};. Even if the loop runs, it will probably always return 1. Again recommend compiling with -Wall and -Werror to root out these sorts of issues.

As a side not, it is better to only push your commits if they successfully compile first or mark the commit/pull request as WIP so others know not to expect a fully working implementation.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.