Heapsort Idea: dos fases: 1. Construccion del heap 2. Output del heap

Slides:



Advertisements
Presentaciones similares
Mr. Redaelli OnlineTaco.com. To Have - Tener Yo tengo – I have Tú tienes – You have (Informal) Usted tiene – You have (Formal) El tiene – He has Ella.
Advertisements

How to Conjugate… SPANISH VERBS.
Game Cluedo: How to Play 1.Your group should have the 21 cards containing 6 cards of suspects, 9 rooms and 6 weapons, a tally card for each member and.
Control en cascada.
What are subject pronouns??? They replace the subject in the sentence. For example: IWe You HeThey She.
Direct Objects & Direct Object Pronouns An English and Spanish Lesson.
Negatives and Questions. Negatives Consider the following sentences: Juan estudia mucho. Marta y Antonio viven en Georgia. Rita y el chico necesitan.
Español la memoria (1): cómo trabajarla bien. Hoy vamos a… mirar escucharpensar hablar memorizar.
Pronombres de objetos directos … …dónde los ponemos y como los usamos.
WALT: talking about dates and saying when your birthday is WILF: To be able to write & understand months and dates in Spanish to get to a Level 2 To be.
Naming Covalent Compounds Day II. Do Now  Fill out the ‘Do Now’ section on your Do Now and Daily Evaluation Sheet  Rellene el 'Do Now' sección de su.
7.  El TDA Diccionario.
Study this picture for 1 minute. Try to remember as much as possible about it. Now tell your partner about the picture. Take it in turns to say something.
Arboles B (búsqueda externa)
Las Preguntas (the questions) Tengo una pregunta… Sí, Juan habla mucho con el profesor en clase. No, Juan no habla mucho en clase. s vo s vo Forming.
Time Telling time is rather easy. You only need to know the numbers up to 59 to be able to tell the time.
Unidad 8 Árboles B Bibliografía: “Algoritmos y Estructuras de datos” de Aguilar y Martinez. Unidad 16 Autor: Ing Rolando Simon Titiosky.
El presente indicativo ESPAÑOL 1. A. What is the present tense? It is when the action of a verb occurs at the moment. Verbs can be divided into two categories:
1 Ordenando. 2 Sorting Input Una secuencia de numeros a 1, a 2, a 3, …, a n Output Una permutación (reorden) a’ 1, a’ 2, a’ 3, …, a’ n de la input, tal.
ECOM-6030 PASOS PARA LA INSTALACIÓN DE EASYPHP Prof. Nelliud D. Torres © - Derechos Reservados.
Essential question: How do I say what I like and what interests/bores me?
¿Cuánto tiempo hace que…? You can ask when something happened in Spanish by using: ¿Cuándo + [preterit verb]…? ¿Cuándo llegaste a la clínica? When did.
OPINION ESSAY
Stamps to be traced. 2 and 3 ALC 21 Hoy es miércoles el 23 de octubre. match the columns writing out the words. 1.Fat 2.Handsome 3.Long 4.Old 5.Pretty.
Tienes un nuevo mensaje…. Ella era una chica timida, llamada Lina, no tenia amigosy solo convivia con su familia especialmente con su madre y su padre…
EQUILIBRIUM OF A PARTICLE IN 2-D Today’s Objectives: Students will be able to : a) Draw a free body diagram (FBD), and, b) Apply equations of equilibrium.
Vocabulario Spanish English Zaragoza Zaragoza Badajoz Badajoz
Question Formation Español Uno.
Español 3 24 de febrero de 2017.
Hoy es el _1 / 4____ de _marzo____ del __2013________
8.  Ordenamiento.
In Spanish, tell whether you agree with the following statements
My Spanish 2 classes are 2nd and 3rd periods
Español 2 2 de mayo de 2017.
Telling Time in Spanish
Double Object Pronouns
PREGUNTAS: Questions and Question Words
Español 1 11 de mayo de 2017 Good morning, thank you for taking my classes today. For the most part the kids are really great. Here is my schedule for.
SER and SUBJECT PRONOUNS
Base de Datos II Almacenamiento.
First Grade Dual High Frequency Words
Review for MIDTERM 2016 What we’ve covered so far…
El Imperfecto Español 2.
More sentences that contain if…
Complete the following sentences using the word bank.
Warm-up Fill in the blank with the correct form of the verb “ser” for each subject (p. 35): 1. Yo _______________ de Savannah. 2. Mis amigas ________.
GRAPHIC MATERIALS 1. GRAPHIC MATERIALS. GRAPHIC MATERIALS 1. GRAPHIC MATERIALS.
Genentech A Discussion Winter 2018Joseph Milner, RSM54011.
¡buenos días!.
Control de Gestión en las Entidades Públicas
-er & -ir Verbs As we saw in the previous presentation, there are three conjugations of verbs in Spanish: -ar, -er, and -ir. -er and -ir verbs are often.
Quasimodo: Tienes que hacer parte D de la tarea..
Telling Time in Spanish
Hace = For/Since.
How to Conjugate… SPANISH VERBS.
UNIVERSIDAD TECNICA DE MACHALA UNIDAD ACADEMICA DE CIENCIAS EMPRESARIALES CARRERA DE ECONOMIA ESTUDENTS: FIRST CONDITIONAL SENTENCES TEACHER: - Calvache.
Vocabulario yellow amarillo and y a sharpener un sacapuntas
Preparacion Hoy es jueves el 3 de diciembre
The Windows File System and Windows Explorer To move around the file system and examine your files or get to one you want (say, to modify, delete or copy.
Indirect Questions First Day on the Job 11 Focus on Grammar 4 Part X, Unit 28 By Ruth Luman, Gabriele Steiner, and BJ Wells Copyright © Pearson Education,
ZERO CONDITIONAL. What is zero conditional? Zero conditional is a structure used to talk about general truths, that is, things that always happen under.
Fundamentals of Web Development - 2 nd Ed.Randy Connolly and Ricardo Hoar Fundamentals of Web DevelopmentRandy Connolly and Ricardo Hoar © 2017 Pearson.
Using Adjectives as Nouns
Para el fin de esta leccíon, se puede:
Arrays Programming COMP102 Prog. Fundamentals I: Arrays / Slide 2 Arrays l An array is a collection of data elements that are of the same type (e.g.,
Welcome to PowerPoint gdskcgdskfcbskjc. Designer helps you get your point across PowerPoint Designer suggests professional designs for your presentation,
Regular –ar (present tense)
Astronomy has really big numbers. Distance between Earth and Sun meters kilometers This is the closest star.
The causative is a common structure in English. It is used when one thing or person causes another thing or person to do something.
Las Preguntas (the questions) Tengo una pregunta… Sí, Juan habla mucho con el profesor en clase. No, Juan no habla mucho en clase. s vo s vo Forming.
Transcripción de la presentación:

Heapsort Idea: dos fases: 1. Construccion del heap 2. Output del heap Para ordenar numeros ascendentemente: mayor valor => mayor prioridad (el mayor esta en la raiz) Heapsort es un procedimiento in-situ

Recordemos Heaps: cambio en la definicion Heap con orden reverso: Para cada nodo x y cada sucesor y de x se cumple que m(x)  m(y), left-complete, significa que los niveles se llenan partiendo por la raíz y cada nivel de izquierda a derecha Implementación en arreglo, donde los nodos se guardan en orden (de izquierda a derecha).

Definamos un segmento de heap Como un segmento de arreglo a[ i..k ] ( 1  i  k <=n ) donde se cumple:         para todo j de {i,...,k}     m(a[ j ])  m(a[ 2j ])     if 2j  k y   m(a[ j ])  m(a[ 2j+1])  if 2j+1  k Si a[i+1..n] es un segmento de heap podemos facilmente convertir a[i…n] en un segmento de heap tambien „hundiendo“ a[ i ].

1. Construccion del Heap: métido simple : insert n-veces Primera Fase: 1. Construccion del Heap: métido simple : insert n-veces Cost0: O(n log n). haciendolo mejor: considere el arreglo a[1 … n ] como un heap que está bien ordenado a la derecha. Los elementos de la mitad izquierda aun no ordenados se dejan “caer” en la siguiente secuencia: a[n div 2] … a[2] a[1] (los elementos a[n] … a[n div 2 +1] están ya en las hojas ) HH The leafs of the heap

Segunda Fase 2. Output del heap heap: costo: O(n log n). Ordered elements Ordered elements 2. Output del heap heap: sacar n-veces el maximo (en la raíz), e intercambiarlo con ultimo elemento del heap, dejarlo caer. El Heap se reduced en un elemento y el mayor queda al final. Repetir este proceso hasta que haya solo un elemento en el heap (el menor) costo: O(n log n).

Cost calculation Sea k = [log n+1] la altura del heap que se está construyendo en la fase 1 ) Para un elemento en el nivel j, suponiendo que los niveles j+1 hasta k estan construidos, el costo máximo de incluirlo en el segmento será: k – j. Además en cada nivel j hay 2j elementos En suma: {j=0,…,k} (k-j)•2j = 2k • {i=0,…,k} i/2i =2 • 2k = O(n).

Ventajas: Este procedimiento de construccion del heap es más rápido! Uso: cuando se requieren solo los m mayores elementos: 1. construccion en O(n) pasos. 2. obteneción de los m mayores elementos en O(m•log n) pasos. costo total : O( n + m•log n).

Addendum: Ordenando con árboles de búsqueda Algorithm: Construccion del árbol de búsqueda (e.g. AVL-tree) con lo elementos que hay que ordenar haciendo n opearciones de inserción. Obtención de los elementos recorriendo el árbol en secuencia InOrder.  Secuencia Ordenada. costo: 1. O(n log n) con AVL-trees, 2. O(n). en total: O(n log n). optimal!

Sorting Externo Problema: ordenar un archivo muy grande guardado en bloques (páginas). eficiencia: numero de acceso a páginas debe minimizarse! Estrategia: Usar un algoritmo que procese los datos en forma secuencial para evitar frecuentes cambios de página: MergeSort!

Forma General para Merge mergesort(S) # retorna el conjunto S ordenado { if(S es vacío o tiene sólo 1 elemento) return(S); else { Dividir S en dos mitades A y B; A'=mergesort(A); B'=mergesort(B); return(merge(A',B')); }

Meregesort en Archivos: Start: se tienen n datos en un archivo g1, divididos en páginas de tamaño b: Page 1: s1,…,sb Page 2: sb+1,…s2b … Page k: s(k-1)b+1 ,…,sn ( k = [n/b]+ ) Si se procesan secuencialmente se hacen k accesos a paginas, no n.

Variacion de MergeSort para external sorting MergeSort: Divide-and-Conquer-Algorithm Para external sorting: sin el paso divide, solo merge. Definicion: run := subsecuencia ordenada dentro de un archivo. Estrategia: by merging increasingly bigger generated runs until everything is sorted.

Algoritmo 1. Step: Generar del input file g1 „starting runs“ y distribuirlas en dos archivos f1 and f2, con el mismo numero de runs (1) en cada uno (for this there are many strategies, later). Ahora: use 4 files f1, f2, g1, g2.

2. Step (main step): while (number of runs > 1) { Merge each two runs from f1 and f2 to a double sized run alternating to g1 und g2, until there are no more runs in f1 and f2. Merge each two runs from g1 and g2 to a double sized run alternating to f1 and f2, until there are no more runs in g1 und g2. } Each loop = two phases

Example: Start: g1: 64, 17, 3, 99, 79, 78, 19, 13, 67, 34, 8, 12, 50 1st. step (length of starting run= 1): f1: 64 | 3 | 79 | 19 | 67 | 8 | 50 f2: 17 | 99 | 78 | 13 | 34 | 12 Main step, 1st. loop, part 1 (1st. Phase ): g1: 17, 64 | 78, 79 | 34, 67 | 50 g2: 3, 99 | 13, 19 | 8, 12 1st. loop, part 2 (2nd. Phase): f1: 3, 17, 64, 99 | 8, 12, 34, 67 | f2: 13, 19, 78, 79 | 50 |

Example continuation 1st. loop, part 2 (2nd. Phase): f1: 3, 17, 64, 99 | 8, 12, 34, 67 | f2: 13, 19, 78, 79 | 50 | 2nd. loop, part 1 (3rd. Phase): g1: 3, 13, 17, 19, 64, 78, 79, 99 | g2: 8, 12, 34, 50, 67 | 2nd. loop, part 2 (4th. Phase): f1: 3, 8, 12, 13, 17, 19, 34, 50, 64, 67, 78, 79, 99 | f2:

Implementation: For each file f1, f2, g1, g2 at least one page of them is stored in principal memory (RAM), even better, a second one might be stored as buffer. Read/write operations are made page-wise.

Costs Page accesses during 1. step and each phase: O(n/b) In each phase we divide the number of runs by 2, thus: Total number of accesses to pages: O((n/b) log n), when starting with runs of length 1. Internal computing time in 1 step and each phase is: O(n). Total internal computing time: O( n log n ).

Two variants of the first step: creation of the start runs A) Direct mixing sort in primary memory („internally“) as many data as possible, for example m data sets  First run of a (fixed!) length m, thus r := n/m starting runs. Then we have the total number of page accesses: O( (n/b) log(r) ).

Two variants of the first step: creation of the start runs B) Natural mixing Creates starting runs of variable length. Advantage: we can take advantage of ordered subsequences that the file may contain Noteworthy: starting runs can be made longer by using the replacement-selection method by having a bigger primary storage !

Replacement-Selection Read m data from the input file in the primary memory (array). repeat { mark all data in the array as „now“. start a new run. while there is a „now“ marked data in the array { select the smallest (smallest key) from all „now“ marked data, print it in the output file, replace the number in the array with a number read from the input file (if there are still some) mark it „now“ if it is bigger or equal to the last outputted data, else mark it as „not now“. } Until there are no data in the input file.

Example: array in primary storage with capacity of 3 The input file has the following data: 64, 17, 3, 99, 79, 78, 19, 13, 67, 34, 8, 12, 50 In the array: („not now“ data written in parenthesis) Runs : 3, 17, 64, 78, 79, 99 | 13, 19, 34, 67 | 8, 12, 50 64 17 3 99 79 78 (19) (13) (67) 19 13 67 34 (8) (12) (50) 8 12 50

Implementation: In an array: At the front: Heap for „now“ marked data, At the back: refilled „not now“ data. Note: all „now“ elements go to the current generated run.

Expected length of the starting runs using the replace-select method: (m = size of the array in the primary storage = number of data that fit into primary storage) by equally probabilities distribution Even bigger if there is some previous sorting!

Multi-way merging Instead of using two input and two output files (alternating f1, f2 and g1, g2) Use k input and k output files, in order to me able to merge always k runs in one. In each step: take the smallest number among the k runs and output it to the current output file.

Cost: In each phase: number of runs is devided by k, Thus, if we have r starting runs we need only logk(r) phases (instead of log2(r)). Total number of accesses to pages: O( (n/b) logk(r) ). Internal computing time for each phase: O(n log2 (k)) Total internal computing time: O( n log2(k) logk(r)) = O( n log2(r) ).