Aks
BAN USER
We can do this in one pass using a pointer and a queue of length 5
pseudo code
function 5th_from_tail( head )
s = queue length 5
s = Null
ptr = head
while ptr != Null
s.push_to_head(ptr)
ptr = ptr -> next
element = s.pop_from_tail()
if element == Null
return "not enough elements"
else
return element
External sorting algorithm, which sorts chunks that each fit in RAM, then merges the sorted chunks together. For example, for sorting 900 megabytes of data using only 100 megabytes of RAM:
Read 100 MB of the data in main memory and sort by some conventional method, like quicksort.
Write the sorted data to disk.
Repeat steps 1 and 2 until all of the data is in sorted 100 MB chunks (there are 900MB / 100MB = 9 chunks), which now need to be merged into one single output file.
Read the first 10 MB (= 100MB / (9 chunks + 1)) of each sorted chunk into input buffers in main memory and allocate the remaining 10 MB for an output buffer. (In practice, it might provide better performance to make the output buffer larger and the input buffers slightly smaller.)
Perform a 9-way merge and store the result in the output buffer. If the output buffer is full, write it to the final sorted file, and empty it. If any of the 9 input buffers gets empty, fill it with the next 10 MB of its associated 100 MB sorted chunk until no more data from the chunk is available. This is the key step that makes external merge sort work externally -- because the merge algorithm only makes one pass sequentially through each of the chunks, each chunk does not have to be loaded completely; rather, sequential parts of the chunk can be loaded as needed.
courtesy :- wikipedia (en.wikipedia.org/wiki/External_sorting )
Using Quicksort..........
def quick_sort(list):
"""Quicksort"""
if list == []:
return []
else:
pivot = list[0]
lesser = quick_sort([x for x in list[1:] if x < pivot])
greater = quick_sort([x for x in list[1:] if x >= pivot])
return lesser + [pivot] + greater
def min_element(list):
sort_list = quick_sort(list)
return sort_list[0]
Pseudo code for Post Order Traversal without Recursion.....
Using Single Stack and single variable...
PostOrderWithIteration(root)
stack stc = empty
prev = null
stc.push(root)
while(!stc.empty()){
curr = stc.top()
if curr == NULL
stc.pop()
//reached at leaf Node
elseif curr->left == NULL && curr->right == NULL
print curr->data
stc.pop()
//left child is already visited so push right
elseif curr->left == prev
stc.push(curr->right)
//left & right both child visited so print value
elseif curr->right == prev
print curr->data
stc.pop()
else
stc.push(curr->left)
prev = curr
I think this is simple Change problem, which can be solved by DP or recursion.
pseudo code for this will be....
Change(array, size, target)
best_change[0] = 0
for c in target
best_change[c] = infinite
for i in size
if c > array[i]
best_change[c-array[i]] + 1 < best_change[c]
best_change[c] = best_change[c- array[i]] + 1
return best_change[target]
and array need not to be sorted....
- Aks July 10, 2012
Array....
- Aks August 26, 2012a long array...where index will be used as hashing.
and it will also take care of duplicate elements, because for that we can increase array[number]
1. enqueue and dequeue will be O(1)---> array[number]++ for enqueue; array[number]-- for dequeue
2. IsNumberPresent --> array[number] > 0 present else not
3. delete--> array[number] =0 to remove all duplicate of this number and itself