Wednesday, October 8, 2014

Algorithms

—Heap - controlled by developer (more bugs?)  malloc(), free(), and realloc()
Can cause “memory leak - means the memory getting so full that software-program crashes” because developer may forget to deallocate the memory in heat.
—Stack - automatic memory for function calls…each function call create one and is deallocated after the function call exists. (less bugs?)

—Pass the pointer to a thing by using & in c.

—In linked list, the head pointer(local to a function) is in stack and the node is in the heap(user created, malloc’d)

—Recursion is solving problem by solving a smaller problem of the same type.

—Recursive algorithm must have a base case. Where there is more than one recursive call, you will often need more than one base case.

Quick Sort
http://www.mycstutorials.com/artiles/sorting/quicksort

Algorithm for partition:

1. while data[index_to_bigger_data] <=data[pivot]
        ++index_to_bigger_data
2. while data[index_to_smaller_data] <=data[pivtot]
        --index_to_smaller_data
3. if index_to_bigger_data < index_to_smaller_data
        swap data[index_to_bigger_data] and data[index_to_smaller_data]
4. While index_to_smaller_data > index_to_bigger_data, go to 1.
5. Swap data[index_to_smaller_data] and data[pivot_index]




Sort Algorithms

Souls of Sort Algorithms
Sort Algorithm Name Sort Algorithm Soul
Selection Sort Scan for min, 2nd min or max
Bubble Sort Swap(a,b) - in-place XOR etc
Insertion Sort array vs. list
Shell Sort example from class
Counting Sort find the most frequent
Merge Sort
Quick Sort Partition is as important as merge() in merge sort -- relationship to BST, recursion



0 comments: