Exemplo n.º 1
0
struct list_node *quicksort(struct list_node *head) {
    if (head == NULL || head->next == NULL) {
        return head;
    }

    struct list_node *middle = find_middle(head);
    struct list_node *head_partitioned = partition(head, middle);

    struct list_node *after_mid = middle->next;
    middle->next = NULL;
    after_mid = quicksort(after_mid);
    head_partitioned = quicksort(head_partitioned);

    struct list_node *final_head = after_mid;
    if (head_partitioned != NULL) {
        struct list_node *last = head_partitioned;
        while (last->next != NULL) {
            last = last->next;
        }
        last->next = final_head;
        final_head = head_partitioned;
    }

    return final_head;
}
int main()
{
    struct stack *ms=NULL;
    
	ms=push(ms, 11);
    ms=push(ms, 22);
    ms=push(ms, 33);
	ms=push(ms, 44);
    ms=push(ms, 55);
    ms=push(ms, 66);
    ms=push(ms, 77);
 	
 	delete_middle();
    printf("Middle Element is %d\n", find_middle());
    printf("Item popped is %d\n", pop(&ms));
    printf("Middle Element is %d\n", find_middle());
    printf("Item popped is %d\n", pop(&ms));
    printf("Middle Element is %d\n", find_middle());
}
Exemplo n.º 3
0
void lfm(int array[], int size){
  if (lfm_active){
    int delta = find_middle(array,size)-width/2;
    lfm_speed[LEFT]=MAX_DELTA*delta/size;
    lfm_speed[RIGHT]=-lfm_speed[LEFT];
	////give adjustments to default speed, speed[LEFT] and  speed[right]
  } else {
    lfm_speed[RIGHT]=lfm_speed[LEFT]=0;
  }
}
Exemplo n.º 4
0
static void merge_sort_divide(register int * a, register int start, register int end) {
	if (start >= end || is_sorted(a, start, end)) return;

	register const int middle = find_middle(start, end);
	// divide the array into 2 parts
	merge_sort_divide(a, start, middle);
	merge_sort_divide(a, middle + 1, end);

	// merge the 2 parts together
	merge(a, start, middle, end);
}
Exemplo n.º 5
0
int main(void)
{
	int a[N], *p, middle;
	printf("Enter a series of %d numbers: ", N);
	for (p = a; p < a + N; p++){
		scanf("%d", &*p);
	}
	middle = find_middle(a, N);
	printf("Middle: %d\n", middle);
	return 0;
}
Exemplo n.º 6
0
/**	The following quicksort algorithm with median partition is adapted from 
	http://eternallyconfuzzled.com/tuts/algorithms/jsw_tut_sorting.aspx
*/
static int find_median_pivot(register int * a, register int start, register int end) {
	register int middle = find_middle(start, end);
	register int pivot;
	
	// find the larger between start and end
	if (*(a + start) > *(a + end)) {
		pivot = start;
	} else {
		pivot = end;
	}
	
	// if the pivot is the largest element 
	if (*(a + pivot) > *(a + middle)) {
		// then change the pivot to middle which is the median
		pivot = middle;
	}
	// else the pivot is already median
	return pivot;
}
Exemplo n.º 7
0
int main(int argc, char **argv) {
    unsigned int i;
    unsigned int limit = 10;
    // Think of `list` as an array variable. In C, an array variable is
    // actually a pointer to the first element of the array.
    Item *list, *current_item;

    // Initialize the list
    current_item = list = allocate_item();

    //printf("\n  Got here. \n");
    init_list(list);

    // Create list items
    for(i = 0; i < limit; i ++) {
        // When creating a new item, set the new item's address to the struct
        // memeber `next_item` and then set `next_item` to `current_item` to advance
        // the list.
        Item *item = allocate_item();
        append_item(current_item, item);
        current_item->value = i;
        current_item = current_item->next_item;
    }

    // Reset the current item pointer to the start of the list so we can print the
    // list to stdout
    current_item = list;
    find_middle(list);
    printf("  Full list");
    print_list(list);

    list = reverse_list(list);
    //delete_item(list, 5);

    printf("\n\n  Full list");
    print_list(list);

    current_item = find_item(list, 4);

    printf("  Searched item: %d", current_item->value);
    // Leak memory.
}