Tags » Sort

Merge Sort Template

public void sortIntegers2(int[] A) {
    // use a shared temp array, the extra memory is O(n) at least
    int[] temp = new int;
    mergeSort(A, 0, A.length - 1, temp);
}
private void mergeSort(int[] A, int start, int end, int temp){
    if(start >= end){
        return;
    }
    int mid = start + (end - start) / 2;
    mergeSort(A, start, mid, temp);
    mergeSort(A, mid + 1, end, temp);
    merge(A, start, mid, end, temp);
}
private void merge(int[] A, int start, int mid, int end, int[] temp){
    int left = start;
    int right = mid + 1;
    int index = start;
    // merge two sorted subarrays in A to temp array
    while(left <= mid && right <= end){
        if(A < A){
            temp = A;
        } else {
            temp = A;
        }
    }
    while(left <= mid){
        temp = A;
    }
    while(right <= mid){
        temp = A;
    }
    // copy temp back to A
    for(index = start; index <= end; index++){
        A = temp;
    }
} 14 more words
Sort

Katie's Homemade Granola

Ingredients:

  • 3 c. oats
  • ½ c. sunflower seeds
  • ½ c. nuts
  • ½ c. coconut*
  • ¼ c. oil (melted coconut oil or butter works great)
  • ¼ c.
  • 12 more words
Sort

Homemade Granola Bars

Ingredients:

  • 1 cup white whole wheat flour
  • 1/2 tsp cinnamon
  • 1/2 tsp baking powder
  • 1/4 tsp salt
  • 2/3 cup coconut oil
  • 2/3 cup maple syrup…
  • 34 more words
Sort

raw hemp and chia seed bar

Ingredients:

  • 3 1/2 cups organic quick rolled oats (gluten-free, if needed)
  • 1/2 cup organic hemp seeds
  • 1/2 cup organic chia seeds
  • 1/4 cup organic flaxseeds (freshly ground)
  • 41 more words
Sort

The Trip A Lovely Sort Of Death Online Game

The trip a lovely sort of death online game

Play the best Shooting Games online at Mousebreaker for free. It’s a duel to the death in this intense action game. 554 more words

High Five

/**
 * @param results a list of <student_id, score>
 * @return find the average of 5 highest scores for each person
 * Map<Integer, Double> (student_id, average_score)
 */
public Map<Integer, Double> highFive(Record[] results) {
    Map<Integer, Double> answer = new HashMap<>();
    Map<Integer, List<Integer>> hash = new HashMap<Integer, List<Integer>>();
    for(Record r : results){
        if(!hash.containsKey(r.id)){
            hash.put(r.id, new ArrayList<Integer>());
        }
        if(hash.get(r.id).size() < 5){
            hash.get(r.id).add(r.score);
        } else {
            int lowest_index = 0;
            for(int i = 1; i < 5; i++){
                if(hash.get(r.id).get(i) < hash.get(r.id).get(lowest_index)){
                    lowest_index = i;
                }
            }
            if(hash.get(r.id).get(lowest_index) < r.score){
                hash.get(r.id).set(lowest_index, r.score);
            }
        }
    }
    for(Map.Entry<Integer, List<Integer>> entry : hash.entrySet()){
        int id = entry.getKey();
        List<Integer> scores = entry.getValue();
        double average = 0.0;
        for(int i = 0; i < scores.size(); i++){
            average += scores.get(i);
        }
        average /= scores.size();
        answer.put(id, average);
    }
    return answer;
}
Sort

K Closest Points

/**
 * @param points a list of points
 * @param origin a point
 * @param k an integer
 * @return the k closest points
 */
public Point[] kClosest(Point[] points, Point origin, int k) {
    final Point global_origin = origin;
    PriorityQueue<Point> priorityqueue = new PriorityQueue<Point>(k, new Comparator<Point>(){
            @Override
            public int compare(Point a, Point b){
                int diff = getDistance(b, global_origin) - getDistance(a, global_origin);
                if(diff == 0){
                    diff = b.x - a.x;
                }
                if(diff == 0){
                    diff = b.y - a.y;
                }
                return diff;
            }
        });
    
    for(int i = 0; i < points.length; i++){
        priorityqueue.offer(points[i]);
        if(priorityqueue.size() > k){
            priorityqueue.poll();
        }
    }
    k = priorityqueue.size();
    Point[] res = new Point[k];
    while(!priorityqueue.isEmpty()){
        res[--k] = priorityqueue.poll();
    }
    return res;
}
private int getDistance(Point a, Point b) {
    return (a.x - b.x) * (a.x - b.x) + (a.y - b.y) * (a.y - b.y);
}
Sort