0% found this document useful (0 votes)
23 views6 pages

RadixSort CRUD Documentation

Radix sort C++

Uploaded by

Hamza Ghumman
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
23 views6 pages

RadixSort CRUD Documentation

Radix sort C++

Uploaded by

Hamza Ghumman
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 6

Radix Sort and CRUD Operations

Program Documentation
Overview

This C++ program demonstrates the use of radix sort for sorting an array of integers and
provides a simple CRUD (Create, Read, Update, Delete) interface for managing a database of
numbers. The program allows the user to:
- Create numbers and add them to the database.
- Read and display all numbers in the database.
- Update an existing number in the database.
- Delete a number from the database.

Includes and Namespace

#include <iostream>
#include <vector>
#include <algorithm>

using namespace std;

- `#include <iostream>`: Provides input and output stream objects.


- `#include <vector>`: Provides the `std::vector` container for dynamic arrays.
- `#include <algorithm>`: Provides algorithms like `std::max_element`.

Global Variables

vector<int> database; // Declare the database vector globally

- `database`: A global vector to store the list of numbers.


Function Definitions

1. Count Sort Function

void countSort(vector<int>& arr, int n, int pos) {


vector<int> output(n);
vector<int> count(10, 0);

for (int i = 0; i < n; i++) {


count[(arr[i] / pos) % 10]++;
}

for (int i = 1; i < 10; i++) {


count[i] += count[i - 1];
}

for (int i = n - 1; i >= 0; i--) {


output[count[(arr[i] / pos) % 10] - 1] = arr[i];
count[(arr[i] / pos) % 10]--;
}

for (int i = 0; i < n; i++) {


arr[i] = output[i];
}
}

- `countSort` performs counting sort on the array `arr` based on the digit represented by
`pos`.
- `pos` determines which digit (units, tens, hundreds, etc.) is used for sorting.
- `output` stores the sorted array temporarily.
- `count` array keeps track of the count of each digit (0-9).

2. Radix Sort Function

void radixSort(vector<int>& arr) {


int k = *max_element(arr.begin(), arr.end());

for (int pos = 1; (k / pos) > 0; pos *= 10) {


countSort(arr, arr.size(), pos);
}
}

- `radixSort` sorts the array `arr` using the counting sort for each digit.
- `k` is the maximum number in the array, which determines the number of digits.
- The loop calls `countSort` for each digit position (units, tens, hundreds, etc.).

3. Create Number Function

void createNumber(int num) {


database.push_back(num);
cout << "Number created successfully!" << endl;
}

- `createNumber` adds a new number to the `database`.

4. Read Numbers Function

void readNumbers() {
cout << "Number Database:" << endl;
for (vector<int>::iterator it = database.begin(); it!= database.end(); ++it) {
cout << *it << " ";
}
cout << endl;
}

- `readNumbers` displays all the numbers in the `database`.

5. Update Number Function

void updateNumber(int oldNum, int newNum) {


for (vector<int>::iterator it = database.begin(); it!= database.end(); ++it) {
if (*it == oldNum) {
*it = newNum;
cout << "Number updated successfully!" << endl;
return;
}
}
cout << "Number not found!" << endl;
}

- `updateNumber` searches for `oldNum` in the `database` and replaces it with `newNum`.

6. Delete Number Function

void deleteNumber(int num) {


for (vector<int>::iterator it = database.begin(); it!= database.end(); ++it) {
if (*it == num) {
database.erase(it);
cout << "Number deleted successfully!" << endl;
return;
}
}
cout << "Number not found!" << endl;
}

- `deleteNumber` searches for `num` in the `database` and deletes it.

Main Function

int main() {
int n;
cout << "Enter the number of numbers: ";
cin >> n;

for (int i = 0; i < n; i++) {


int num;
cout << "Enter number " << i + 1 << ": ";
cin >> num;
createNumber(num);
}

radixSort(database);
while (true) {
cout << "CURD Operations:" << endl;
cout << "1. Create Number" << endl;
cout << "2. Read Numbers" << endl;
cout << "3. Update Number" << endl;
cout << "4. Delete Number" << endl;
cout << "5. Exit" << endl;
cout << "Enter your choice: ";
int choice;
cin >> choice;

switch (choice) {
case 1:
cout << "Enter number: ";
int num;
cin >> num;
createNumber(num);
break;
case 2:
readNumbers();
break;
case 3:
cout << "Enter old number: ";
int oldNum;
cin >> oldNum;
cout << "Enter new number: ";
int newNum;
cin >> newNum;
updateNumber(oldNum, newNum);
break;
case 4:
cout << "Enter number to delete: ";
int delNum;
cin >> delNum;
deleteNumber(delNum);
break;
case 5:
return 0;
}
}

return 0;
}

- The `main` function starts by asking the user to input the number of elements and the
elements themselves, which are added to the `database`.
- It then sorts the `database` using `radixSort`.
- An infinite loop provides a menu for CRUD operations:
1. **Create Number**: Adds a new number to the database.
2. **Read Numbers**: Displays all numbers in the database.
3. **Update Number**: Updates an existing number in the database.
4. **Delete Number**: Deletes a number from the database.
5. **Exit**: Exits the program.

Summary

This program allows the user to manage a database of integers with CRUD operations and
uses radix sort to sort the database. It provides a simple and interactive console interface
for managing and displaying the sorted list of numbers.

You might also like