0% found this document useful (0 votes)
4 views13 pages

Sortowaniec

The document contains a C++ program that implements various sorting algorithms, including insertion sort for different data types (integers, floats, and characters). It allows the user to choose specific tasks related to sorting, such as sorting a full array, a subarray, and counting comparisons and shifts during sorting. Additionally, it includes functionality to read from an input file and write sorted results to an output file.

Uploaded by

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

Sortowaniec

The document contains a C++ program that implements various sorting algorithms, including insertion sort for different data types (integers, floats, and characters). It allows the user to choose specific tasks related to sorting, such as sorting a full array, a subarray, and counting comparisons and shifts during sorting. Additionally, it includes functionality to read from an input file and write sorted results to an output file.

Uploaded by

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

#include <iostream>

#include <vector>

#include <fstream>

using namespace std;

void sortAndPrint(vector<int>& arr) {

for (int i = 1; i < arr.size(); i++) {

int key = arr[i];

int j = i - 1;

while (j >= 0 && arr[j] > key) {

arr[j + 1] = arr[j];

j--;

arr[j + 1] = key;

for (int i = 0; i < arr.size(); i++) {

cout << arr[i] << " ";

cout << endl;

void sortAndPrint(vector<float>& arr) {

for (int i = 1; i < arr.size(); i++) {

float key = arr[i];

int j = i - 1;
while (j >= 0 && arr[j] > key) {

arr[j + 1] = arr[j];

j--;

arr[j + 1] = key;

for (int i = 0; i < arr.size(); i++) {

cout << arr[i] << " ";

cout << endl;

void sortAndPrint(vector<char>& arr) {

for (int i = 1; i < arr.size(); i++) {

char key = arr[i];

int j = i - 1;

while (j >= 0 && arr[j] > key) {

arr[j + 1] = arr[j];

j--;

arr[j + 1] = key;

for (int i = 0; i < arr.size(); i++) {

cout << arr[i] << " ";

}
cout << endl;

int main() {

int zadanie = 0;

cout << "Podaj zadanie:";

cin >> zadanie;

cin.ignore();

switch (zadanie) {

default: {

cout << "nie ma takiego zadania!" << endl;

break;

case 1: {

vector<int> arr = { 5, 2, 9, 1, 5, 6 };

cout << "Tablica przed sortowaniem: ";

sortAndPrint(arr);

break;

case 2: {

vector<float> arr = { 5.1, 2.3, 9.7, 1.4, 5.5, 6.6 };

cout << "Tablica przed sortowaniem: ";

sortAndPrint(arr);
break;

case 3: {

vector<int> arr = { 5, 2, 9, 1, 5, 6 };

cout << "Tablica przed sortowaniem: ";

for (int i = 0; i < arr.size(); i++) {

cout << arr[i] << " ";

cout << endl;

for (int i = 1; i < arr.size(); i++) {

int key = arr[i];

int j = i - 1;

while (j >= 0 && arr[j] < key) {

arr[j + 1] = arr[j];

j--;

arr[j + 1] = key;

cout << "Tablica po sortowaniu: ";

for (int i = 0; i < arr.size(); i++) {

cout << arr[i] << " ";

}
cout << endl;

break;

case 4: {

vector<int> arr = { 5, 2, 9, 1, 5, 6, 3, 8 };

cout << "Tablica przed sortowaniem: ";

for (int i = 0; i < arr.size(); i++) {

cout << arr[i] << " ";

cout << endl;

int start, end;

cout << "Podaj indeks 1:";

cin >> start;

cout << "Podaj indeks 2:";

cin >> end;

for (int i = start + 1; i <= end; i++) {

int key = arr[i];

int j = i - 1;

while (j >= start && arr[j] > key) {

arr[j + 1] = arr[j];

j--;

}
arr[j + 1] = key;

cout << "Tablica po sortowaniu fragmentu: ";

for (int i = 0; i < arr.size(); i++) {

cout << arr[i] << " ";

break;

case 5: {

vector<int> arr = { 5, 2, 9, 1, 5, 6, 3, 8 };

cout << "Tablica przed sortowaniem: ";

for (int i = 0; i < arr.size(); i++) {

cout << arr[i] << " ";

cout << endl;

int comparisons = 0, shifts = 0;

for (int i = 3; i <= 6; i++) {

int key = arr[i];

int j = i - 1;

while (j >= 2 && arr[j] > key) {

comparisons++;

arr[j + 1] = arr[j];
shifts++;

j--;

comparisons++;

arr[j + 1] = key;

shifts++;

cout << "Tablica po sortowaniu fragmentu: ";

for (int i = 0; i < arr.size(); i++) {

cout << arr[i] << " ";

cout << endl;

cout << "Liczba porównań: " << comparisons << endl;

cout << "Liczba przesunięć: " << shifts << endl;

break;

case 6: {

vector<char> arr = { 'z', 'a', 'm', 'k', 'b', 'y', 'o', 'c' };

cout << "Tablica przed sortowaniem: ";

sortAndPrint(arr);

break;

}
case 7: {

vector<int> arr = { 1, 2, 3, 4, 5 };

bool isSorted = true;

for (size_t i = 1; i < arr.size(); ++i) {

if (arr[i - 1] > arr[i]) {

isSorted = false;

break;

if (isSorted) {

cout << "Array is already sorted." << endl;

else {

cout << "Array is not sorted." << endl;

break;

case 8: {

vector<vector<int>> arr = {

{5, 3, 8, 6},

{2, 7, 1, 4, 9},

{9, 0, 3, 2, 1, 8}

};
for (size_t i = 0; i < arr.size(); ++i) {

for (size_t j = 1; j < arr[i].size(); ++j) {

int key = arr[i][j];

size_t k = j;

while (k > 0 && arr[i][k - 1] > key) {

arr[i][k] = arr[i][k - 1];

--k;

arr[i][k] = key;

for (size_t i = 0; i < arr.size(); ++i) {

for (size_t j = 0; j < arr[i].size(); ++j) {

cout << arr[i][j] << " ";

cout << endl;

break;

case 9: {

size_t size;

cout << "Enter the size of the array: ";

cin >> size;


int* arr = new int[size];

cout << "Enter " << size << " elements: ";

for (size_t i = 0; i < size; ++i) {

cin >> arr[i];

for (size_t i = 1; i < size; ++i) {

int key = arr[i];

size_t j = i;

while (j > 0 && arr[j - 1] > key) {

arr[j] = arr[j - 1];

--j;

arr[j] = key;

cout << "Sorted array: ";

for (size_t i = 0; i < size; ++i) {

cout << arr[i] << " ";

cout << endl;

delete[] arr;
break;

case 10: {

size_t size;

cin >> size;

int* arr = new int[size];

for (size_t i = 0; i < size; ++i) {

cin >> arr[i];

for (size_t i = 1; i < size; ++i) {

int key = arr[i];

size_t j = i;

while (j > 0 && arr[j - 1] > key) {

arr[j] = arr[j - 1];

--j;

arr[j] = key;

for (size_t i = 0; i < size; ++i) {

cout << arr[i] << " ";

}
cout << endl;

delete[] arr;

break;

case 11: {

ifstream inputFile("input.txt");

ofstream outputFile("output.txt");

if (!inputFile.is_open() || !outputFile.is_open()) {

cerr << "Nie mo¿na otworzyæ plików." << endl;

return 1;

vector<int> arr;

int num;

while (inputFile >> num) {

arr.push_back(num);

for (size_t i = 1; i < arr.size(); ++i) {

int key = arr[i];

size_t j = i;

while (j > 0 && arr[j - 1] > key) {

arr[j] = arr[j - 1];


--j;

arr[j] = key;

for (const auto& value : arr) {

outputFile << value << "

You might also like