div-bargali / Data-Structures-and-Algorithms

Data Structures and Algorithms implemented In Python, C, C++, Java or any other languages. Aimed to help strengthen the concepts of DSA. Give a Star 🌟 if it helps you.
MIT License
270 stars 396 forks source link

Addition of Readme file contents for each item and addition of Cycle Sort to C++ Algorithm Sorting part etc... #870

Closed ji-sang-geun closed 3 years ago

ji-sang-geun commented 3 years ago

Pull Request Template

What have you Changed(must)

First, we have filled in most of the readme files for each item in the C++ language. (Excluding pattern Searching and already filled Heap) For example. - Bubble Sort Algorithm that sorts the data by comparing the n-1st data and the nth data like the first data and the second data, the second data and the third data, exchanging the order. Time complexity = O(n^2)

Second, we added cycle sort in C++'s sorting algorithm. (Here is an excerpt from the cycle sort code.)

include<bits/stdc++.h>

using namespace std; // Sorting the array, Returns the number of writes. int cycle_sort(int arr[], int size_of_arr) { int writes, item, pos; writes = 0;

// Repeat to figure out the cycle's cycle
for (int i = 0; i < size_of_arr - 1; i++) {
    item = arr[i];
    pos = i;
    for (int j = i + 1; j < size_of_arr; j++) {
        if (arr[j] < item) pos += 1;
    }
    // If pos is equal to cyclestart, it is passed to "continue" because it has reached the beginning of the cycle's cycle.
    if (pos == i) continue;

    // Since itemand arr[pos] are the same, it means they have the same value, so pos is increased
    while (item == arr[pos]) pos += 1;
    swap(arr[pos], item); // swap arr[pos] and item
    writes += 1;

    // Rotate the remaining cycles.
    while (pos != i) {
        // Find where to put the item
        pos = i;
        for (int j = i + 1; j < size_of_arr; j++) {
            if (arr[j] < item) pos += 1;
        }
        // Since itemand arr[pos] are the same, it means they have the same value, so pos is increased
        while (item == arr[pos]) pos += 1;
        swap(arr[pos], item);
        writes += 1;
    }
}
return writes; // Return writes to the main function.

}

Third, For some code, we have simplified the code. like c++ -> sort -> MergeSort,

include <bits/stdc++.h>

using namespace std;

void merge(int arr[], int low, int mid, int high){ int i, j, k; int n1 = mid + 1 - low; int n2 = high - mid; int l[n1], r[n2]; for(int i=0; i<n1; i++) l[i]=arr[low+i]; // left for(int j=0; j<n2; j++) r[j]=arr[mid+j+1]; // right i=0; j=0; k=low;

while(i<n1 && j<n2){ if(l[i]<=r[j]) arr[k++]=l[i++]; else arr[k++]=r[j++]; } while(i<n1) arr[k++]=l[i++]; while(j<n2) arr[k++]=r[j++]; }

void mergeSort(int arr[],int low,int high){ // sorting int mid; if(low<high){ mid=low+(high-low)/2;

mergeSort(arr,low,mid);
mergeSort(arr,mid+1,high);
merge(arr,low,mid,high);

} }

int main() { //enter number of elements int n; cin>>n; int arr[n]; //enter elemetns for(int i=0;i<n;i++) cin>>arr[i]; //called Function mergeSort(arr,0,n-1); //after Sort array for(int i=0;i<n;i++) cout<<arr[i]<<" ";

return 0;

}

fourth, In the case of JumpSearch, there is a problem that you must enter the sorted number when entering, but by adding mergesort, you do not necessarily enter the sorted number when entering.

int main() { int n, search; cout << "Enter no. of elements in an array: "; cin >> n; int arr[n]; cout << "Enter elements in an array: "; for(int i=0;i<n;i++) cin >> arr[i]; mergeSort(arr,0,n-1); // Merge sort is used because the processing speed is rather fast. cout << "Enter a number to be searched: "; cin >> search; int result = jump_search(arr,n,search); // Jump Search if(result == -1) cout << search << " is not present in the array." << endl; else cout << search << " is present at index no " << result+1 << endl;

return 0;

}

lastly, In the c++ part (parts excluding pattern Searching), files that were not commented were searched for and added comments. It also modified files without the .cpp extension.

Extra, in the c++ Data structures related to graphs are divided into "graph" folder and "Graph" folder. So you have to merge the two.

Issue no.(must) - ##531

Pr will be closed and marked as spam. If issue number not found or issue was assigned to someone else. Marking as spam can block your account from HacktoberFest.

Self Check(Tick After Making pull Request)

README - How to Contribute