Assembly bubble sort swap

I think I’d use pointers into the current position into the list, instead of an index that needs to be scaled every time you use it: mov esi, offset list top: mov edi, esi inner: mov eax, [edi] mov edx, [edi+4] cmp eax, edx jle no_swap mov [edi+4], eax mov [edi], edx no_swap: add edi, … Read more

c++ sort with structs

You should use C++’s standard sort function, std::sort, declared in the <algorithm> header. When you sort using a custom sorting function, you have to provide a predicate function that says whether the left-hand value is less than the right-hand value. So if you want to sort by name first, then by ID, then by amount … Read more

Bubble sort implementation in PHP? [duplicate]

function bubble_sort($arr) { $size = count($arr)-1; for ($i=0; $i<$size; $i++) { for ($j=0; $j<$size-$i; $j++) { $k = $j+1; if ($arr[$k] < $arr[$j]) { // Swap elements at indices: $j, $k list($arr[$j], $arr[$k]) = array($arr[$k], $arr[$j]); } } } return $arr; } For example: $arr = array(1,3,2,8,5,7,4,0); print(“Before sorting”); print_r($arr); $arr = bubble_sort($arr); print(“After sorting … Read more

Simple bubble sort c#

No, your algorithm works but your Write operation is misplaced within the outer loop. int[] arr = { 800, 11, 50, 771, 649, 770, 240, 9 }; int temp = 0; for (int write = 0; write < arr.Length; write++) { for (int sort = 0; sort < arr.Length – 1; sort++) { if (arr[sort] … Read more

Sort 2D matrix in C

#include <stdio.h> #include <stdlib.h> #include <time.h> #include <string.h> void bubble_sort(void *base, size_t n, size_t size, int (*cmp)(const void*, const void *)); int cmp_int(const void *a, const void *b){ int x = *(const int *)a; int y = *(const int *)b; return x < y ? -1 : x > y; } int main() { int … Read more