Example #1
0
int List_bubble_sort(List *list, List_compare cmp)
{
  // int sorted = 1;

  if(List_count(list) <= 1) {
    return 0; // already sorted
  }

   
  int count = List_count(list);

  do {
    int current = 0;
    int reducer = 0; 
    LIST_FOREACH_LIMIT(list, first, next, cur, count, reducer) {
      if(cur->next) {
        if(cmp(cur->value, cur->next->value) > 0) {
          ListNode_swap(cur, cur->next);
          reducer = current;
        }
      }
      current++;
    }
    count = reducer;
  } while(count != 0);
   
   return 0;
}
Example #2
0
int List_bubble_sort(List *list, List_compare cmp)
{
	assert(list != NULL && "list can't be NULL");
	assert(cmp != NULL && "cmp can't be NULL");

	int n = List_count(list);

	if(n <= 1) {
		return 0; // already sorted
	}

	do {
		int j = 0;
		int new_n = 0;

		LIST_FOREACH(list, first, next, cur) {
			if(j == n - 1) break;
		
			if(cmp(cur->value, cur->next->value) > 0) {
				ListNode_swap(cur, cur->next);

				new_n = j + 1;
			}

			j++;
		}

		n = new_n;

	} while(n != 0);

	return 0;
}
Example #3
0
int List_bubble_sort(List *list, List_compare cmp) {
	int sorted = 1;

	if(List_count(list) <= 1) {
		return 0; /* List is sorted, 1 or 0 element */
	}

	do{
		sorted = 1;
		LIST_FOREACH(list, first, next, cur) {
			if(cur->next) {
				if(cmp(cur->value, cur->next->value) > 0) {
					ListNode_swap(cur, cur->next);
					sorted = 0;
				}
			}
		}
	} while(!sorted);

	return 0;
}
int List_bubble_sort(List *list, List_compare cmp) {
  if(List_count(list) <= 1) {
    return 0;
  }

  int swapped = 0;

  do {
    swapped = 0;

    LIST_FOREACH(list, first, next, cur) {
      if(cur->next) {
        if(cmp(cur->value, cur->next->value) > 0) {
          ListNode_swap(cur, cur->next);
          swapped = 1;
        }
      }
    }
  } while(swapped != 0);

  return 0;
}
Example #5
0
int List_bubble_sort(List *unsorted, List_val_compare cmp) {
    check(unsorted != NULL, "Tried to sort NULL.");
    if(List_count(unsorted) <= 1) return 0;

    int sorted = 1;

    do {
        sorted = 1;

        LIST_FOREACH(unsorted, first, next, cur) {
            if(cur->next) {
                if(cmp(cur->value, cur->next->value) > 0) {
                    ListNode_swap(cur, cur->next);
                    sorted = 0;
                }
            }
        }
    } while(!sorted);

    return 0;

error:
    return 1;
}
int List_bubble_sort(List* list, List_compare cmp)
{
  int sorted = 1;

  if (List_count(list) <= 1) {
    // Already sorted.
    return 0;
  }

  do {
    sorted = 1;
    LIST_FOREACH(list, first, next, cur) {
      if (cur->next) {
        if (cmp(cur->value, cur->next->value) > 0) {
          ListNode_swap(cur, cur->next);
          sorted = 0;
        }
      }
    }

  } while(!sorted);

  return 0;
}
Example #7
0
int List_bubble_sort(List *list, List_compare cmp)
{
  int sorted = 1;

  if (List_Count(list) <= 1) {
    return 0;
  }

  do {
    sorted = 1;
    // Remember we have a custom function to iterate
    // over list items!
    LIST_FOREACH(list, first, next, cur) {
      if (cur->next) {
        if(cmp(cur->value, cur->next->value) > 0) {
          ListNode_swap(cur, cur->next);
          sorted = 0;
        }
      }
    }
  } while(!sorted);

  return 0;
}