{"id":27247339,"url":"https://github.com/matrix278/algorithms","last_synced_at":"2025-04-10T22:49:31.787Z","repository":{"id":56523358,"uuid":"227387594","full_name":"Matrix278/algorithms","owner":"Matrix278","description":"Algorithms","archived":false,"fork":false,"pushed_at":"2020-10-31T15:11:53.000Z","size":1389,"stargazers_count":3,"open_issues_count":0,"forks_count":15,"subscribers_count":1,"default_branch":"master","last_synced_at":"2025-04-10T22:49:27.704Z","etag":null,"topics":["algorithm","bubble-sorting","hacktoberfest","insertion-sorting","linear-search","quicksort","selection-sort","sort","sorting","sorting-algorithm"],"latest_commit_sha":null,"homepage":"","language":"Java","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/Matrix278.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2019-12-11T14:39:31.000Z","updated_at":"2024-03-20T11:03:54.000Z","dependencies_parsed_at":"2022-08-15T20:20:28.508Z","dependency_job_id":null,"html_url":"https://github.com/Matrix278/algorithms","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Matrix278%2Falgorithms","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Matrix278%2Falgorithms/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Matrix278%2Falgorithms/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Matrix278%2Falgorithms/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/Matrix278","download_url":"https://codeload.github.com/Matrix278/algorithms/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":248312173,"owners_count":21082637,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["algorithm","bubble-sorting","hacktoberfest","insertion-sorting","linear-search","quicksort","selection-sort","sort","sorting","sorting-algorithm"],"created_at":"2025-04-10T22:49:31.187Z","updated_at":"2025-04-10T22:49:31.774Z","avatar_url":"https://github.com/Matrix278.png","language":"Java","readme":"# Algorithms\n\n## Bubble sort (Сортировка пузырьком)\nBubble sort compares each pair of elements in a list and swaps them if they are out of order until the list is sorted. Like insertion sort, bubble sort is a comparison algorithm and runs in O(n²) time, making it an inefficient algorithm for larger lists.\n\n### Visual\n![Bubble sorting](bubbleSort/bubbleSort.gif)\n\n### Code:\n```javascript\nfunction bubbleSort(arr) {\n    var done = false;\n  \n    while(!done){\n      done = true;\n        for (var i = 0; i \u003c arr.length - 1; i++) {\n            if (arr[i] \u003e arr[i+1]) {\n                var temp = arr[i];\n                arr[i] = arr[i+1];\n                arr[i+1] = temp;\n                done = false;\n            }\n        }\n    }\n    return arr;\n}\n\nbubbleSort([2, 6, 1, 9, 3, 8, 5, 4, 7]);\n```\n\n## Selection sort (Сортировка выбором)\nA sort algorithm that repeatedly searches remaining items to find the least one and moves it to its final location. The run time is O(n²), where n is the number of elements. The number of swaps is O(n).\n\n### Visual\n![Selection sorting](selectionSort/selectionSort.gif)\n\n### Code:\n```javascript\nfunction selectionSort(arr){\n    for (let i = 0; i \u003c arr.length; i++) {\n        let min = i;\n        for (let j = i + 1; j \u003c arr.length; j++) {\n            if (arr[min] \u003e arr[j]) {\n                min = j;\n            }\n        }\n        if (min !== i) {\n            let tmp = arr[i];\n            arr[i] = arr[min];\n            arr[min] = tmp;\n        }\n    }\n    return arr;\n}\n\nselectionSort([1, 5, 6, 2, 3, 8, 4, 7, 9]);\n```\n\n## Insertion sort (Сортировка вставками)\nInsertion sort is a simple sorting algorithm that works the way we sort playing cards in our hands. This sort is based on the idea that one element from the input elements is consumed in each iteration to find its correct position i.e, the position to which it belongs in a sorted array. Since is the first element has no other element to be compared with, it remains at its position. It is much less efficient on large lists than more advanced algorithms such as quicksort, heapsort, or merge sort.\n\n### Visual\n![Insertion sorting](insertionSort/insertionSort.gif)\n\n### Code:\n```javascript\nfunction insertionSort(arr){\n  for (var i = 1; i \u003c arr.length; i++){\n    if (arr[i] \u003c arr[0]){\n      //move current element to the first position\n      arr.unshift(arr.splice(i,1)[0]);\n    }else if (arr[i] \u003e arr[i-1]){\n      //leave current element where it is\n      continue;\n    }else{\n      //find where element should go\n      for (var j = 1; j \u003c i; j++){\n        if (arr[i] \u003e arr[j-1] \u0026\u0026 arr[i] \u003c arr[j]){\n          //move element\n          arr.splice(j,0,arr.splice(i,1)[0]);\n        }\n      }\n    }\n  }\n  return arr;\n}\n\ninsertionSort([3, 0, 2, 5, 6, 4, 1]);\n```\n\n## Linear search (Линейный поиск)\nIn this algorithm, you can stop when the item is found and then there is no need to look further. In the best case, you could get lucky and the item you are looking at maybe at the first position in the array! But in the worst case, you would have to look at each and every item before you find the item at the last place or before you realize that the item is not in the array. The complexity therefore of the linear search is: O(n). If the element to be searched presides on the the first memory block then the complexity would be: O(1).\n\n### Visual\n![Linear search](linearSearch/linearSearch.gif)\n\n### Code:\n```javascript\nfunction linearSearch(arr, item) {\n  // Go through all the elements of arr to look for item.\n  for (var i = 0; i \u003c arr.length; i++) {\n    if (arr[i] === item) { // Found it!\n      return i;\n    }\n  }\n  // Item not found in the array.\n  return null;\n}\n\nlinearSearch([1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20], 13);\n```\n\n## Binary search (Бинарный поиск)\nBinary search is an algorithm used to find a particular item in a **sorted list**. It’s essential for the list to be sorted beforehand or the algorithm won’t be applicable. If you’ve ever dealt with binary search trees, this concept of this algorithm is similar. In each step, the algorithm compares the input element x with the value of the middle element in array. If the values match, return the index of middle. Otherwise, if x is less than the middle element, then the algorithm recurs for left side of middle element, else recurs for right side of middle element.\n\n### Visual\n![Binary search](binarySearch/binarySearch.gif)\n\n### Code:\n```javascript\nfunction binarySearch(arr, target) {\n    let first = 0;\n    let last = arr.length - 1;\n    let position = -1;\n    let found = false;\n    let middle;\n\n    while (found === false \u0026\u0026 first \u003c= last) {\n        middle = Math.floor((first + last) / 2);\n        if (arr[middle] == target) {\n            found = true;\n            position = middle;\n        }else if (arr[middle] \u003e target){\n            last = middle - 1;\n        }else{\n            first = middle + 1;\n        }\n    }\n    return position;\n}\n\nbinarySearch([1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20], 13);\n```\n\n## Quicksort (Быстрая сортировка)\nQuick sort is an efficient divide and conquer sorting algorithm. Average case time complexity of Quick Sort is O(nlog(n)) with worst case time complexity being O(n^2).\n\nThe steps involved in Quick Sort are:\n\n- Choose an element to serve as a pivot, in this case, the last element of the array is the pivot.\n- Partitioning: Sort the array in such a manner that all elements less than the pivot are to the left, and all elements greater than the pivot are to the right.\n- Call Quicksort recursively, taking into account the previous pivot to properly subdivide the left and right arrays. (A more detailed - - explanation can be found in the comments below)\n\n### Visual\n![Quicksort](quickSort/quickSort2.gif)\n![Quicksort](quickSort/quickSort.gif)\n\n### Code:\n```javascript\nfunction swap(items, leftIndex, rightIndex){\n    var temp = items[leftIndex];\n    items[leftIndex] = items[rightIndex];\n    items[rightIndex] = temp;\n}\nfunction partition(items, left, right) {\n    var pivot   = items[Math.floor((right + left) / 2)], //middle element\n        i       = left, //left pointer\n        j       = right; //right pointer\n    while (i \u003c= j) {\n        while (items[i] \u003c pivot) {\n            i++;\n        }\n        while (items[j] \u003e pivot) {\n            j--;\n        }\n        if (i \u003c= j) {\n            swap(items, i, j); //sawpping two elements\n            i++;\n            j--;\n        }\n    }\n    return i;\n}\n\nfunction quickSort(items, left, right) {\n    var index;\n    if (items.length \u003e 1) {\n        index = partition(items, left, right); //index returned from partition\n        if (left \u003c index - 1) { //more elements on the left side of the pivot\n            quickSort(items, left, index - 1);\n        }\n        if (index \u003c right) { //more elements on the right side of the pivot\n            quickSort(items, index, right);\n        }\n    }\n    return items;\n}\n\nvar items = [5,3,7,6,2,9];\nquickSort(items, 0, items.length - 1);\n```\n\n## Counting sort (Сортировка подсчётом)\nCounting sort is a sorting algorithm that sorts the elements of an array by counting the number of occurrences of each unique element in the array. It works on a limited set of elements. The count is stored in an auxiliary array and the sorting is done by mapping the count as an index of the auxiliary array. It runs in O(n + k) where n is the number of elements in the input array and k is the maximum key.\n\n### Code:\n```javascript\nfunction countingSort(arr, max) {\n    const count = new Array(max + 1);\n    for (let i = 0; i \u003c max + 1; ++i) count[i] = 0;\n\n    for (let i = 0; i \u003c arr.length; ++i) count[arr[i]] += 1;\n\n    let total = 0;\n    for (let i = 0; i \u003c count.length; ++i) {\n        const prevTotal = total;\n        total += count[i];\n        count[i] = prevTotal;\n    }\n\n    const output = arr.slice();\n    for (let i = 0; i \u003c arr.length; ++i) {\n        output[count[arr[i]]] = arr[i];\n        count[arr[i]] += 1;\n    }\n    return output;\n}\n\ncountingSort([1, 2, 3, 2, 7, 9, 1, 0], 9);\n```\n\n## Merge sort (Сортировка слиянием)\n\n## Heapsort (Пирамидальная сортировка)\n","funding_links":[],"categories":[],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmatrix278%2Falgorithms","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fmatrix278%2Falgorithms","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmatrix278%2Falgorithms/lists"}