# Sorting Arrays Bubble

**URL:** <https://forum.codewithmosh.com/t/sorting-arrays-bubble/27337>\
**Category:** C++\
**Created:** [June 25, 2024, 7:25am UTC](https://forum.codewithmosh.com/t/sorting-arrays-bubble/27337 "2024-06-25T07:25:12Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![connor](https://avatars.discourse-cdn.com/v4/letter/c/e99b99/32.png) [@connor](https://forum.codewithmosh.com/u/connor)\
**Post date:** [June 25, 2024, 7:25am UTC](https://forum.codewithmosh.com/t/sorting-arrays-bubble/27337/1 "2024-06-25T07:25:12Z")

</div>

General question about this code because I’m trying to fully understand what’s going on. Question at bottom…

void swap(int numbers, int i, int j) {  
int temp = numbers[i];  
numbers[i] = numbers[j];  
numbers[j] = temp;  
}

void sort(int numbers, int size) {  
for (int pass = 0; pass \< size; pass++) {  
for (int i = 1; i \< size; i++)  
if (numbers[i] \< numbers[i - 1])  
swap(numbers[i], numbers[i - 1]);

```
}

```

}

int main()  
{  
int numbers = { 7, 4, 2, 1 };  
sort(numbers, size(numbers));  
for (int number : numbers)  
cout \<\< number;

```
return 0;

```

}

“for (int number : numbers)”  
So, my question involves this line, and I am curious why I have to loop over the numbers array to get a desired result because if I remove this line I get hexadecimal values?

---

<div class="post-metadata">

**Author:** ![lucidlear](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.codewithmosh.com/lucidlear/32/5477_2.png) [@lucidlear](https://forum.codewithmosh.com/u/lucidlear)\
**Post date:** [June 25, 2024, 7:43pm UTC](https://forum.codewithmosh.com/t/sorting-arrays-bubble/27337/2 "2024-06-25T19:43:50Z")

</div>

Hi, this line: `for (int number : numbers)` is used to print each individual element of the array in its sorted order. If we don’t iterate through the array and access its elements individually, but instead try to print the `numbers` array directly, we will get a hexadecimal value. This is because printing the array name directly outputs the memory address of the array, not the actual elements. An array in C++ is represented as a pointer to the first element of the array in memory, hence the memory address is shown

---

<div class="post-metadata">

**Author:** ![connor](https://avatars.discourse-cdn.com/v4/letter/c/e99b99/32.png) [@connor](https://forum.codewithmosh.com/u/connor)\
**Post date:** [June 25, 2024, 8:55pm UTC](https://forum.codewithmosh.com/t/sorting-arrays-bubble/27337/3 "2024-06-25T20:55:24Z")

</div>

Oooooh okay cool this makes much more sense now! Thank you!
