├── .gitignore ├── .vscode └── settings.json ├── Binary-exponentiation ├── application.cpp ├── iterative.cpp └── recursive.cpp ├── Binary-search ├── binarysearch.cpp ├── binarysearchWithSorted.cpp ├── binarysearchwithvector.cpp ├── testingbinarysearch.c ├── testingbinarysearch.cpp └── testingbinarysearch.js ├── LICENSE ├── Linear-search ├── getintoit.c └── getintoit.cpp ├── README.md ├── Stack ├── stack.c └── stack.cpp ├── bubble-sort ├── bubblesort.c ├── bubblesort.cpp └── bubblesortwitharray.cpp ├── collaborators ├── console.js ├── reverse polish notation └── reverse-polish-notation.cpp └── selection-sort ├── selectionsort.c ├── selectionsort.cpp └── selectionsort.js /.gitignore: -------------------------------------------------------------------------------- 1 | *.exe 2 | *.bin 3 | .cph/ -------------------------------------------------------------------------------- /.vscode/settings.json: -------------------------------------------------------------------------------- 1 | { 2 | "files.associations": { 3 | "iostream": "cpp" 4 | } 5 | } 6 | -------------------------------------------------------------------------------- /Binary-exponentiation/application.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | long long binpow(long long a, long long b, long long m) 4 | { 5 | a %= m; 6 | long long res = 1; 7 | while (b > 0) 8 | { 9 | if (b & 1) 10 | res = res * a % m; 11 | a = a * a % m; 12 | b >>= 1; 13 | } 14 | return res; 15 | } 16 | int main() 17 | { 18 | long long a, b, m; 19 | cin >> a >> b >> m; 20 | cout << binpow(a, b, m); 21 | return 0; 22 | } -------------------------------------------------------------------------------- /Binary-exponentiation/iterative.cpp: -------------------------------------------------------------------------------- 1 | // write a program to calculate a^b in O(logn) time 2 | #include 3 | using namespace std; 4 | int power(int a, int b) 5 | { 6 | int result = 1; 7 | while (b > 0) 8 | { 9 | if (b % 2 == 1) 10 | result *= a; 11 | a *= a; 12 | b /= 2; 13 | } 14 | return result; 15 | } 16 | int main() 17 | { 18 | int a, b; 19 | cin >> a >> b; 20 | cout << power(a, b); 21 | return 0; 22 | } 23 | -------------------------------------------------------------------------------- /Binary-exponentiation/recursive.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int power(int a, int b) 5 | { 6 | if (b == 0) 7 | return 1; 8 | if (b == 1) 9 | return a; 10 | int tmp = power(a, b / 2); 11 | int result = tmp * tmp; 12 | if (b % 2 == 1) 13 | result *= a; 14 | return result; 15 | } 16 | int main() 17 | { 18 | int a, b; 19 | cin >> a >> b; 20 | cout << power(a, b); 21 | return 0; 22 | } -------------------------------------------------------------------------------- /Binary-search/binarysearch.cpp: -------------------------------------------------------------------------------- 1 | // binary search algorithm 2 | // Time complexity: O(logn) 3 | // Space complexity: O(1) 4 | #include 5 | using namespace std; 6 | int main() 7 | { 8 | int n; 9 | cout << "Enter the number of elements in the array: "; 10 | cin >> n; 11 | int a[n]; 12 | cout << "Enter the elements of the array: "; 13 | for (int i = 0; i < n; i++) 14 | { 15 | cin >> a[i]; 16 | } 17 | int key; 18 | cout << "Enter the element to be searched: "; 19 | cin >> key; 20 | int s = 0; 21 | int e = n; 22 | while (s <= e) 23 | { 24 | int mid = (s + e) / 2; 25 | if (a[mid] == key) 26 | { 27 | cout << "The element is present at index " << mid << endl; 28 | return 0; 29 | } 30 | else if (a[mid] > key) 31 | { 32 | e = mid - 1; 33 | } 34 | else 35 | { 36 | s = mid + 1; 37 | } 38 | } 39 | cout << "The element is not present in the array" << endl; 40 | return 0; 41 | } -------------------------------------------------------------------------------- /Binary-search/binarysearchWithSorted.cpp: -------------------------------------------------------------------------------- 1 | // write binary search algorithm after sorting the array 2 | #include 3 | using namespace std; 4 | int main() 5 | { 6 | int n; 7 | cout << "Enter the number of elements in the array: "; 8 | cin >> n; 9 | int a[n]; 10 | cout << "Enter the elements of the array: "; 11 | for (int i = 0; i < n; i++) 12 | { 13 | cin >> a[i]; 14 | } 15 | sort(a, a + n); 16 | int key; 17 | cout << "Enter the element to be searched: "; 18 | cin >> key; 19 | int s = 0; 20 | int e = n; 21 | while (s <= e) 22 | { 23 | int mid = (s + e) / 2; 24 | if (a[mid] == key) 25 | { 26 | cout << "The element is present at index " << mid << endl; 27 | return 0; 28 | } 29 | else if (a[mid] > key) 30 | { 31 | e = mid - 1; 32 | } 33 | else 34 | { 35 | s = mid + 1; 36 | } 37 | } 38 | cout << "The element is not present in the array" << endl; 39 | return 0; 40 | } -------------------------------------------------------------------------------- /Binary-search/binarysearchwithvector.cpp: -------------------------------------------------------------------------------- 1 | // binary search algorithm using vector 2 | #include 3 | using namespace std; 4 | int binarySearch(vector &a, int key) 5 | { 6 | int s = 0; 7 | int e = a.size() - 1; 8 | while (s <= e) 9 | { 10 | int mid = (s + e) / 2; 11 | if (a[mid] == key) 12 | { 13 | return mid; 14 | } 15 | else if (a[mid] > key) 16 | { 17 | e = mid - 1; 18 | } 19 | else 20 | { 21 | s = mid + 1; 22 | } 23 | } 24 | return -1; 25 | } 26 | int main() 27 | { 28 | int n; 29 | cout << "Enter the number of elements in the array: "; 30 | cin >> n; 31 | vector a; 32 | cout << "Enter the elements of the array: "; 33 | for (int i = 0; i < n; i++) 34 | { 35 | int x; 36 | cin >> x; 37 | a.push_back(x); 38 | } 39 | int key; 40 | cout << "Enter the element to be searched: "; 41 | cin >> key; 42 | int index = binarySearch(a, key); 43 | if (index != -1) 44 | { 45 | cout << "The element is present at index " << index << endl; 46 | } 47 | else 48 | { 49 | cout << "The element is not present in the array" << endl; 50 | } 51 | return 0; 52 | } -------------------------------------------------------------------------------- /Binary-search/testingbinarysearch.c: -------------------------------------------------------------------------------- 1 | // binary search algorithm 2 | // 1. sort the array 3 | // 2. find the middle element 4 | // 3. if the middle element is the target, return the index 5 | // 4. if the middle element is greater than the target, search the left half 6 | // 5. if the middle element is less than the target, search the right half 7 | // 6. repeat steps 2-5 until the target is found or the array is empty 8 | #include 9 | #include 10 | int binarySearch(int arr[], int target, int size); 11 | int main() 12 | { 13 | int arr[] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10}; 14 | int target = 5; 15 | int size = sizeof(arr) / sizeof(arr[0]); 16 | int index = binarySearch(arr, target, size); 17 | if (index == -1) 18 | { 19 | printf("Target not found"); 20 | } 21 | else 22 | { 23 | printf("Target found at index %d", index); 24 | } 25 | return 0; 26 | } 27 | int binarySearch(int arr[], int target, int size) 28 | { 29 | int left = 0; 30 | int right = size - 1; 31 | while (left <= right) 32 | { 33 | int middle = (left + right) / 2; 34 | if (arr[middle] == target) 35 | { 36 | return middle; 37 | } 38 | else if (arr[middle] > target) 39 | { 40 | right = middle - 1; 41 | } 42 | else 43 | { 44 | left = middle + 1; 45 | } 46 | } 47 | return -1; 48 | } -------------------------------------------------------------------------------- /Binary-search/testingbinarysearch.cpp: -------------------------------------------------------------------------------- 1 | // binary search algorithm 2 | // Time complexity: O(logn) 3 | // Space complexity: O(1) 4 | 5 | #include 6 | using namespace std; 7 | int binarysearch(int arr[], int n, int key) 8 | { 9 | int s = 0; 10 | int e = n; 11 | while (s <= e) 12 | { 13 | int mid = (s + e) / 2; 14 | if (arr[mid] == key) 15 | { 16 | return mid; 17 | } 18 | else if (arr[mid] > key) 19 | { 20 | e = mid - 1; 21 | } 22 | else 23 | { 24 | s = mid + 1; 25 | } 26 | } 27 | return -1; 28 | } 29 | int main() 30 | { 31 | int n; 32 | cin >> n; 33 | int arr[n]; 34 | for (int i = 0; i < n; i++) 35 | { 36 | cin >> arr[i]; 37 | } 38 | int key; 39 | cin >> key; 40 | cout << binarysearch(arr, n, key) << endl; 41 | return 0; 42 | } 43 | -------------------------------------------------------------------------------- /Binary-search/testingbinarysearch.js: -------------------------------------------------------------------------------- 1 | //binary search algorithm 2 | //binary search is a search algorithm that finds the position of a target value within a sorted array. 3 | //binary search compares the target value to the middle element of the array. 4 | //if they are not equal, the half in which the target cannot lie is eliminated and the search continues on the remaining half, again taking the middle element to compare to the target value, and repeating this until the target value is found. 5 | //if the search ends with the remaining half being empty, the target is not in the array. 6 | //binary search runs in logarithmic time in the worst case, making O(log n) comparisons, where n is the number of elements in the array, and therefore is much faster than linear search O(n) which makes at most O(n) comparisons, where n is the number of elements in the array. 7 | //if the search space is reduced by half at each comparison, then the total number of comparisons will be the number of times one can halve n before getting a value that is less than or equal to one. 8 | //that is if n is 16, then the maximum number of times one can halve 16 to get a value less than or equal to one is four. 9 | //if n is 32, then the maximum number of times one can halve 32 to get a value less than or equal to one is five. 10 | //this can be computed using the following formula: 11 | //log base 2 of n equals the number of times one can halve n to get a value less than or equal to one. 12 | const binarySearch = (arr, target) => { 13 | let start = 0; 14 | let end = arr.length - 1; 15 | let middle = Math.floor((start + end) / 2); 16 | while (arr[middle] !== target && start <= end) { 17 | if (target < arr[middle]) end = middle - 1; 18 | else start = middle + 1; 19 | middle = Math.floor((start + end) / 2); 20 | } 21 | return arr[middle] === target ? middle : -1; 22 | }; 23 | console.log(binarySearch([2, 5, 6, 9, 13, 15, 28, 30], 103)); 24 | //output: -1 25 | //Time complexity: O(log n) 26 | //Space complexity: O(1) 27 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2023 Your Tech Guy 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /Linear-search/getintoit.c: -------------------------------------------------------------------------------- 1 | // linear search 2 | #include 3 | #include 4 | #include 5 | void main() 6 | { 7 | int a[10], i, n, flag = 0; 8 | // clrscr(); 9 | printf("Enter the number of elements in the array: "); 10 | scanf("%d", &n); 11 | printf("Enter the elements of the array: "); 12 | for (i = 0; i < n; i++) 13 | { 14 | scanf("%d", &a[i]); 15 | } 16 | printf("Enter the element to be searched: "); 17 | scanf("%d", &n); 18 | for (i = 0; i < n; i++) 19 | { 20 | if (a[i] == n) 21 | { 22 | flag = 1; 23 | break; 24 | } 25 | } 26 | if (flag == 1) 27 | printf("Element found at position %d", i + 1); 28 | else 29 | printf("Element not found"); 30 | getch(); 31 | } 32 | -------------------------------------------------------------------------------- /Linear-search/getintoit.cpp: -------------------------------------------------------------------------------- 1 | // linear search algorithm 2 | // 1. Start from the leftmost element of arr[] and one by one compare x with each element of arr[] 3 | // 4. The time complexity of above algorithm is O(n) 4 | // 5. Linear search is rarely used practically because other search algorithms such as the binary search algorithm and hash tables allow significantly faster searching comparison to Linear search 5 | // 6. Linear search is used for searching in unsorted list 6 | // 7. Linear search is also called sequential search 7 | // 8. Linear search is the simplest, least efficient, basic, intuitive, elementary, straight forward and basic search algorithm 8 | 9 | #include 10 | using namespace std; 11 | int main() 12 | { 13 | int n; 14 | cout << "Enter the size of array: "; 15 | cin >> n; 16 | int arr[n]; 17 | cout << "Enter the elements of array: "; 18 | for (int i = 0; i < n; i++) 19 | { 20 | cin >> arr[i]; 21 | } 22 | int x; 23 | cout << "Enter the element to be searched: "; 24 | cin >> x; 25 | int flag = 0; 26 | for (int i = 0; i < n; i++) 27 | { 28 | if (arr[i] == x) 29 | { 30 | cout << "Element found at index: " << i << endl; 31 | flag = 1; 32 | break; 33 | } 34 | } 35 | if (flag == 0) 36 | { 37 | cout << "Element not found" << endl; 38 | } 39 | return 0; 40 | } -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Data structure and algorithms with Tanbir Hossain Ramim 2 | ### 1. [Linear Search algorithm explained](https://www.youtube.com/watch?v=CBnZVCMkhk0&t=1s) 3 | ### 2. [Binary Search algorithm explained](https://www.youtube.com/watch?v=vijevL4WZiU) 4 | ### 3. [Selection Sort algorithm explained](https://www.youtube.com/watch?v=n2XmUaKEgp0) 5 | ### 4. [Bubble Sort algorithm explained](https://www.youtube.com/watch?v=8kifw28KFyA) 6 | -------------------------------------------------------------------------------- /Stack/stack.c: -------------------------------------------------------------------------------- 1 | // stack data structure 2 | #include 3 | #include 4 | #define MAX 10 5 | int stack[MAX]; 6 | int top = -1; 7 | void push(int); 8 | int pop(); 9 | int main() 10 | { 11 | int i, n, x; 12 | printf("Enter the number of elements to be pushed: "); 13 | scanf("%d", &n); 14 | for (i = 0; i < n; i++) 15 | { 16 | printf("Enter the element to be pushed: "); 17 | scanf("%d", &x); 18 | push(x); 19 | } 20 | printf("The elements in the stack are: "); 21 | for (i = 0; i < n; i++) 22 | { 23 | printf("%d ", pop()); 24 | } 25 | return 0; 26 | } 27 | void push(int x) 28 | { 29 | if (top == MAX - 1) 30 | { 31 | printf("Stack Overflow"); 32 | exit(0); 33 | } 34 | else 35 | { 36 | top++; 37 | stack[top] = x; 38 | } 39 | } 40 | int pop() 41 | { 42 | int x; 43 | if (top == -1) 44 | { 45 | printf("Stack Underflow"); 46 | exit(0); 47 | } 48 | else 49 | { 50 | x = stack[top]; 51 | top--; 52 | return x; 53 | } 54 | } 55 | -------------------------------------------------------------------------------- /Stack/stack.cpp: -------------------------------------------------------------------------------- 1 | // stack 2 | // stack is a linear data structure which follows a particular order in which the operations are performed. 3 | // The order may be LIFO(Last In First Out) or FILO(First In Last Out). 4 | // Mainly the following three basic operations are performed in the stack: 5 | // Push: Adds an item in the stack. If the stack is full, then it is said to be an Overflow condition. 6 | // Pop: Removes an item from the stack. The items are popped in the reversed order in which they are pushed. If the stack is empty, then it is said to be an Underflow condition. 7 | // Peek or Top: Returns top element of stack. 8 | 9 | #include 10 | using namespace std; 11 | #define MAX 1000 12 | class Stack 13 | { 14 | int top; 15 | 16 | public: 17 | int a[MAX]; 18 | Stack() 19 | { 20 | top = -1; 21 | } 22 | bool push(int x); 23 | int pop(); 24 | int peek(); 25 | bool isEmpty(); 26 | bool isFull(); 27 | }; 28 | bool Stack::push(int x) 29 | { 30 | if (top >= (MAX - 1)) 31 | { 32 | cout << "Stack Overflow"; 33 | return false; 34 | } 35 | else 36 | { 37 | a[++top] = x; 38 | cout << x << " pushed into stack" << endl; 39 | return true; 40 | } 41 | } 42 | int Stack::pop() 43 | { 44 | if (top < 0) 45 | { 46 | cout << "Stack Underflow"; 47 | return 0; 48 | } 49 | else 50 | { 51 | int x = a[top--]; 52 | return x; 53 | } 54 | } 55 | int Stack::peek() 56 | { 57 | if (top < 0) 58 | { 59 | cout << "Stack is Empty"; 60 | return 0; 61 | } 62 | else 63 | { 64 | int x = a[top]; 65 | return x; 66 | } 67 | } 68 | bool Stack::isEmpty() 69 | { 70 | return (top < 0); 71 | } 72 | bool Stack::isFull() 73 | { 74 | return (top >= MAX - 1); 75 | } 76 | int main() 77 | { 78 | class Stack s; 79 | s.push(10); 80 | s.push(20); 81 | s.push(30); 82 | cout << s.pop() << " Popped from stack" << endl; 83 | cout << "Top element is " << s.peek() << endl; 84 | cout << "Stack is empty " << s.isEmpty() << endl; 85 | cout << "Stack is full " << s.isFull() << endl; 86 | return 0; 87 | } 88 | -------------------------------------------------------------------------------- /bubble-sort/bubblesort.c: -------------------------------------------------------------------------------- 1 | // bubble sort algorithm in C 2 | #include 3 | #include 4 | #include 5 | #define MAX 100 6 | void bubble_sort(int a[], int n) 7 | { 8 | int i, j, temp; 9 | for (i = 0; i < n; i++) 10 | { 11 | for (j = 0; j < n - 1; j++) 12 | { 13 | if (a[j] > a[j + 1]) 14 | { 15 | temp = a[j]; 16 | a[j] = a[j + 1]; 17 | a[j + 1] = temp; 18 | } 19 | } 20 | } 21 | } 22 | int main() 23 | { 24 | int a[MAX], n, i; 25 | clock_t start, end; 26 | double total_time; 27 | printf("Enter the number of elements: "); 28 | scanf("%d", &n); 29 | printf("Enter the elements: "); 30 | for (i = 0; i < n; i++) 31 | { 32 | scanf("%d", &a[i]); 33 | } 34 | start = clock(); 35 | bubble_sort(a, n); 36 | end = clock(); 37 | total_time = ((double)(end - start)) / CLOCKS_PER_SEC; 38 | printf("Sorted array: "); 39 | for (i = 0; i < n; i++) 40 | { 41 | printf("%d ", a[i]); 42 | } 43 | printf(" Time taken: %f", total_time); 44 | return 0; 45 | } 46 | -------------------------------------------------------------------------------- /bubble-sort/bubblesort.cpp: -------------------------------------------------------------------------------- 1 | // bubble sort algorithm in c++ using vector, swap 2 | #include 3 | using namespace std; 4 | int main() 5 | { 6 | vector v; 7 | int x; 8 | cout << "Enter the size of the array: "; 9 | cin >> x; 10 | cout << "Enter the elements of the array: "; 11 | for (int i = 0; i < x; i++) 12 | { 13 | int y; 14 | cin >> y; 15 | v.push_back(y); 16 | } 17 | int n = v.size(); 18 | for (int i = 0; i < n; i++) 19 | { 20 | for (int j = 0; j < n - i - 1; j++) 21 | { 22 | if (v[j] > v[j + 1]) 23 | { 24 | swap(v[j], v[j + 1]); 25 | } 26 | } 27 | } 28 | for (int i = 0; i < n; i++) 29 | { 30 | cout << v[i] << " "; 31 | } 32 | return 0; 33 | } -------------------------------------------------------------------------------- /bubble-sort/bubblesortwitharray.cpp: -------------------------------------------------------------------------------- 1 | 2 | #include 3 | using namespace std; 4 | int main() 5 | { 6 | int n; 7 | cout << "Enter the size of the array: "; 8 | cin >> n; 9 | int arr[n]; 10 | cout << "Enter the elements of the array: "; 11 | for (int i = 0; i < n; i++) 12 | { 13 | cin >> arr[i]; 14 | } 15 | for (int i = 0; i < n; i++) 16 | { 17 | for (int j = 0; j < n - i - 1; j++) 18 | { 19 | if (arr[j] > arr[j + 1]) 20 | { 21 | swap(arr[j], arr[j + 1]); 22 | } 23 | } 24 | } 25 | for (int i = 0; i < n; i++) 26 | { 27 | cout << arr[i] << " "; 28 | } 29 | return 0; 30 | } 31 | -------------------------------------------------------------------------------- /collaborators: -------------------------------------------------------------------------------- 1 | // just to clarifying things if it goes fine! 2 | -------------------------------------------------------------------------------- /console.js: -------------------------------------------------------------------------------- 1 | // this si for testing purpose 2 | console.log("testing correctly!"); 3 | -------------------------------------------------------------------------------- /reverse polish notation/reverse-polish-notation.cpp: -------------------------------------------------------------------------------- 1 | // reverse polish notation using stack for multiple digit numbers 2 | #include 3 | using namespace std; 4 | int main() 5 | { 6 | string s; 7 | cin >> s; 8 | stack st; 9 | for (int i = 0; i < s.length(); i++) 10 | { 11 | if (s[i] >= '0' && s[i] <= '9') 12 | { 13 | st.push(s[i] - '0'); 14 | } 15 | else 16 | { 17 | int op2 = st.top(); 18 | st.pop(); 19 | int op1 = st.top(); 20 | st.pop(); 21 | switch (s[i]) 22 | { 23 | case '+': 24 | st.push(op1 + op2); 25 | break; 26 | case '-': 27 | st.push(op1 - op2); 28 | break; 29 | case '*': 30 | st.push(op1 * op2); 31 | break; 32 | case '/': 33 | st.push(op1 / op2); 34 | break; 35 | case '^': 36 | st.push(pow(op1, op2)); 37 | break; 38 | case '%': 39 | st.push(op1 % op2); 40 | break; 41 | } 42 | } 43 | } 44 | cout << st.top(); 45 | return 0; 46 | } -------------------------------------------------------------------------------- /selection-sort/selectionsort.c: -------------------------------------------------------------------------------- 1 | // selection sort algorithm 2 | // 1. Find the smallest item in the array and exchange it with the first entry 3 | // 2. Find the next smallest item and exchange it with the second entry 4 | // 3. Continue in this way until the entire array is sorted 5 | // 4. This method is called selection sort because it works by repeatedly selecting the smallest remaining item 6 | // 5. The running time of selection sort is quadratic 7 | // 6. It makes ~ N^2/2 compares and ~ N exchanges 8 | // 7. Selection sort is insensitive to input 9 | // 8. It is useful when memory write is a costly operation 10 | // 9. It is also useful when the input is in the form of a stream 11 | 12 | #include 13 | #include 14 | int main() 15 | { 16 | int i, j, min, temp, n; 17 | int *a; 18 | printf("Enter the number of elements: "); 19 | scanf("%d", &n); 20 | a = (int *)malloc(n * sizeof(int)); 21 | printf("Enter the elements: "); 22 | for (i = 0; i < n; i++) 23 | scanf("%d", &a[i]); 24 | for (i = 0; i < n - 1; i++) 25 | { 26 | min = i; 27 | for (j = i + 1; j < n; j++) 28 | { 29 | if (a[j] < a[min]) 30 | min = j; 31 | } 32 | temp = a[i]; 33 | a[i] = a[min]; 34 | a[min] = temp; 35 | } 36 | printf("Sorted array: "); 37 | for (i = 0; i < n; i++) 38 | printf("%d ", a[i]); 39 | return 0; 40 | } -------------------------------------------------------------------------------- /selection-sort/selectionsort.cpp: -------------------------------------------------------------------------------- 1 | // selection sort algorithm 2 | // 1. Find the smallest item in the array and exchange it with the first entry 3 | // 2. Find the next smallest item and exchange it with the second entry 4 | // 3. Continue in this way until the entire array is sorted 5 | // 4. This method is called selection sort because it works by repeatedly selecting the smallest remaining item 6 | // 5. The running time of selection sort is quadratic 7 | // 6. It makes ~ N^2/2 compares and ~ N exchanges 8 | // 7. Selection sort is insensitive to input 9 | // 8. It is useful when memory write is a costly operation 10 | // 9. It is also useful when the input is in the form of a stream 11 | #include 12 | using namespace std; 13 | int main() 14 | { 15 | int n; 16 | cout << "Enter the number of elements in the array: "; 17 | cin >> n; 18 | int a[n]; 19 | cout << "Enter the elements of the array: "; 20 | for (int i = 0; i < n; i++) 21 | { 22 | cin >> a[i]; 23 | } 24 | for (int i = 0; i < n; i++) 25 | { 26 | int min = i; 27 | for (int j = i + 1; j < n; j++) 28 | { 29 | if (a[j] < a[min]) 30 | { 31 | min = j; 32 | } 33 | } 34 | swap(a[i], a[min]); 35 | } 36 | cout << "The sorted array is: "; 37 | for (int i = 0; i < n; i++) 38 | { 39 | cout << a[i] << " "; 40 | } 41 | return 0; 42 | } 43 | -------------------------------------------------------------------------------- /selection-sort/selectionsort.js: -------------------------------------------------------------------------------- 1 | // selection sort algorithm 2 | const selectionSort = (arr) => { 3 | for (let i = 0; i < arr.length; i++) { 4 | let min = i; 5 | for (let j = i + 1; j < arr.length; j++) { 6 | if (arr[j] < arr[min]) { 7 | min = j; 8 | } 9 | } 10 | if (min !== i) { 11 | [arr[i], arr[min]] = [arr[min], arr[i]]; 12 | } 13 | } 14 | return arr; 15 | }; 16 | --------------------------------------------------------------------------------