> For the complete documentation index, see [llms.txt](https://42-guide.gitbook.io/42-guide/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://42-guide.gitbook.io/42-guide/piscine-life/c01/ft_sort_int_tab.md).

# ft\_sort\_int\_tab

**Objective:**

Create a function that sorts an array of integers in ascending order.

**Turn-in Directory:** `ex08/`

**Files to Turn In:** `ft_sort_int_tab.c`

**Allowed Functions:** None

**Prototype:**

```c
void ft_sort_int_tab(int *tab, int size);
```

***

#### **Detailed Explanation Using Provided Code:**

Your implementation of `ft_sort_int_tab` sorts the array using the **Bubble Sort** algorithm. Let’s break it down step by step:

[Detailed information about other algorithms you can find here.](https://app.gitbook.com/o/wKHRGV4JZuhJtfJ92jQu/s/2xy0FLS2hVSRnArNrOOw/~/changes/11/algorithms)

***

#### **Code Walkthrough:**

**1. Variables:**

* `i`: Tracks how many passes through the array have been completed.
* `j`: Iterates through the unsorted portion of the array in each pass.
* `temp`: A temporary variable used to swap two elements.

***

**2. Outer Loop (`while (i < size - 1)`):**

* The `i` loop ensures that the sorting process is repeated until all elements are in ascending order.
* After each pass, the largest unsorted element is "bubbled" to its correct position in the array, so the loop only needs to process `size - i - 1` elements in subsequent passes.

***

**3. Inner Loop (`while (j < size - i - 1)`):**

* The `j` loop compares adjacent elements in the array and swaps them if they are out of order (`tab[j] > tab[j + 1]`).
* This process ensures that the largest remaining element in the unsorted portion moves to its correct position by the end of the inner loop.

void ft\_sort\_int\_tab(int \*tab, int size) { int i; int j; int temp;

```c
void	ft_sort_int_tab(int *tab, int size)
{
	int	i;
	int	j;
	int	temp;

	i = 0;
	while (i < size - 1)
	{
		j = 0;
		while (j < size - i)
		{
			if (tab[j] > tab[j + 1])
			{
				temp = tab[j];
				tab[j] = tab[j + 1];
				tab[j + 1] = temp;
			}
			j++;
		}
		i++;
	}
}
```

***

**4. Swapping Mechanism:**

* If `tab[j]` is greater than `tab[j + 1]`, the two elements are swapped using the `temp` variable:

  ```c
  temp = tab[j];
  tab[j] = tab[j + 1];
  tab[j + 1] = temp;
  ```
* This ensures the smaller element moves closer to the start of the array.

***

#### **Algorithm Behavior (Bubble Sort):**

**Example Input:**

`tab = [5, 3, 8, 6, 2]`

* `size = 5`

**Pass-by-Pass Breakdown:**

1. **Pass 1 (i = 0):**
   * Compare `5` and `3` → Swap → `[3, 5, 8, 6, 2]`
   * Compare `5` and `8` → No swap
   * Compare `8` and `6` → Swap → `[3, 5, 6, 8, 2]`
   * Compare `8` and `2` → Swap → `[3, 5, 6, 2, 8]`
2. **Pass 2 (i = 1):**
   * Compare `3` and `5` → No swap
   * Compare `5` and `6` → No swap
   * Compare `6` and `2` → Swap → `[3, 5, 2, 6, 8]`
3. **Pass 3 (i = 2):**
   * Compare `3` and `5` → No swap
   * Compare `5` and `2` → Swap → `[3, 2, 5, 6, 8]`
4. **Pass 4 (i = 3):**
   * Compare `3` and `2` → Swap → `[2, 3, 5, 6, 8]`
5. **Pass 5 (i = 4):**
   * The array is already sorted; no comparisons are needed.

**Final Output:**

`tab = [2, 3, 5, 6, 8]`

***

#### **Time Complexity:**

1. **Worst Case (Unsorted Array):**
   * **O(n²)** because the algorithm compares all pairs of elements for every pass.
   * Example: `tab = [5, 4, 3, 2, 1]`
2. **Best Case (Already Sorted Array):**
   * **O(n)** if optimized (by breaking out of the loop early when no swaps occur), but your implementation doesn’t include this optimization.
3. **Average Case:**
   * **O(n²)** due to repeated passes and comparisons.

***

#### **Space Complexity:**

* **O(1)** because it sorts the array in place without using extra memory.

***

#### **Edge Cases to Test:**

1. **Empty Array:**
   * Input: `[]`
   * Expected Output: No changes (function should handle it gracefully).
2. **Array with One Element:**
   * Input: `[42]`
   * Expected Output: `[42]`
3. **Already Sorted Array:**
   * Input: `[1, 2, 3, 4, 5]`
   * Expected Output: `[1, 2, 3, 4, 5]`
4. **Reverse Sorted Array:**
   * Input: `[5, 4, 3, 2, 1]`
   * Expected Output: `[1, 2, 3, 4, 5]`
5. **Array with Repeated Numbers:**
   * Input: `[4, 2, 4, 1, 4]`
   * Expected Output: `[1, 2, 4, 4, 4]`


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://42-guide.gitbook.io/42-guide/piscine-life/c01/ft_sort_int_tab.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
