Skip to content

Commit

Permalink
[ivt] 25
Browse files Browse the repository at this point in the history
  • Loading branch information
Headary committed Feb 1, 2022
1 parent 6ac823c commit 2419255
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 8 deletions.
6 changes: 3 additions & 3 deletions ivt/25-arrays-sort.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,15 +23,15 @@
### Procházení pole
- využití \inlistc{for} smyčky

\lstinputlisting[language=C, float, caption=Příklad procházení pole]{../source_codes/25-array-traversing.c}
\lstinputlisting[style=c, caption=Příklad procházení pole]{../source_codes/25-array-traversing.c}

## Vícerozměrná pole
- pole o více rozměrech, pole polí
- každý prvek pole je další pole
- \inlistc{int matrix[height][width];}
- stejné čtení a zápis dat jako při jedné dimenzi

\lstinputlisting[language=C, float, caption=Inicializace vícerozměrného pole]{../source_codes/25-matrix-init.c}
\lstinputlisting[style=c, caption=Inicializace vícerozměrného pole]{../source_codes/25-matrix-init.c}

\begin{table}[htbp]
\renewcommand{\arraystretch}{1.25}
Expand Down Expand Up @@ -89,4 +89,4 @@ Bubble sort & bublinkové řazení & $n$ & $n^2$ & $n^2$ & $1$ & Ano & zámě
\caption{Porovnání řadících algoritmů}
\end{table}

\lstinputlisting[language=C, float, caption=Implementace bubble sortu v C]{../source_codes/25-sort-bubble.c}
\lstinputlisting[style=c, caption=Implementace bubble sortu v C]{../source_codes/25-sort-bubble.c}
14 changes: 10 additions & 4 deletions ivt/source_codes/25-sort-bubble.c
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#include <stdio.h>
#include <stdbool.h>

// Swap pointers
void swap(int *xp, int *yp)
Expand All @@ -8,12 +9,17 @@ void swap(int *xp, int *yp)
*yp = temp;
}

// A function to implement bubble sort
// Function to implement bubble sort
void bubbleSort(int arr[], int n) {
for (int i = 0; i < n-1; i++)
for (int i = 0; i < n-1; i++) {
bool swapped = false;
for (int j = 0; j < n-i-1; j++)
if (arr[j] > arr[j+1])
if (arr[j] > arr[j+1]) {
swap(&arr[j], &arr[j+1]);
swapped = true;
}
if (!swapped) break; // Break if already sorted
}
}

// Function to print an array
Expand All @@ -26,7 +32,7 @@ void printArray(int arr[], int size)

// Driver program to test above functions
int main() {
int arr[] = {64, 34, 25, 12, 22, 11, 90};
int arr[] = {11, 64, 34, 25, 12, 22, 90};
int n = sizeof(arr)/sizeof(arr[0]);
bubbleSort(arr, n);
printf("Sorted array: \n");
Expand Down
Binary file added ivt/source_codes/25-sort-bubble.o
Binary file not shown.
8 changes: 7 additions & 1 deletion ivt/template.cls
Original file line number Diff line number Diff line change
Expand Up @@ -91,5 +91,11 @@
numberbychapter
}

\newcommand\inlistc[1]{\lstinline[language=C]{#1}}
\lstdefinestyle{c}{
language=C,
float=htbp,
morekeywords={printf, puts, putchar, fwrite, getchar, gets, scanf, size_t, FILE}
}

\newcommand\inlistc[1]{\lstinline[style=c]{#1}}
\newcommand\inlisth[1]{\lstinline[language=HTML]{#1}}

0 comments on commit 2419255

Please sign in to comment.