• ¿Documento equivocado? Cámbialo gratis
  • Escrito por estudiantes que aprobaron
  • Inmediatamente disponible después del pago
  • Leer en línea o como PDF
Vender
¿Dónde estudias?
Tu idioma
Document preview thumbnail
Vista previa 4 fuera de 154 páginas
Examen

Solutions for Data Structures And Algorithm Analysis In C++ 4th Edition by Mark A. Weiss, Chapters 1-12

Document preview thumbnail
Vista previa 4 fuera de 154 páginas

Solutions for Data Structures And Algorithm Analysis In C++ 4th Edition by Mark A. Weiss, Chapters 1-12. Chapter 1 Programming: A General Overview, Chapter 2 Algorithm Analysis, Chapter 3 Lists, Stacks, and Queues, Chapter 4 Trees, Chapter 5 Hashing, Chapter 6 Priority Queues (Heaps), Chapter 7 Sorting, Chapter 8 The Disjoint Sets Class, Chapter 9 Graph Algorithms, Chapter 10 Algorithm Design Techniques, Chapter 11 Amortized Analysis, Chapter 12 Advanced Data Structures and Implementation

Vista previa del contenido

Data Structures and Algorithm Analysis
ST
in C++ – 4th Edition
UV

SOLUTIONS
IA

MANUAL
_A
PP

Mark A. Weiss
RO
VE
Comprehensive Solutions Manual for

Instructors and Students
D?

© Mark A. Weiss
??
All rights reserved. Reproduction or distribution without permission is prohibited.




©STUDYSTREAM

, CHAPTER 1
ST
Introduction
1.1
/*
UV
Exercise 1.1
Selection of integers with k = N/2
select1 => sorting and selecting
select2 => keeping top k
*/

#include <iostream>
IA
#include <ctime>
#include <cmath>
#include <vector>
#include <algorithm>
using namespace std;
_A

void sort(vector<int> & vec)
{ // bubble sort ascending
bool sorted = false;
PP
while (!sorted)
{
sorted = true;
for (auto i = 1; i < vec.size(); i++)
{
if (vec[i-1]> vec[i])
RO
{
swap(vec[i],vec[i-1]);
sorted = false;
}
}
}
}
VE
void sortDec(vector<int> & vec)
{ // bubble sort descending
bool sorted = false;
while (!sorted)
{
D?
sorted = true;
for (auto i = 1; i < vec.size(); i++)
{
if (vec[i-1]< vec[i])
{
swap(vec[i],vec[i-1]);
sorted = false;
??
}
}
}
}

, int select1(vector<int> nums)
{
int k = (nums.size()+1)/2;
ST
sort(nums);
return nums[k];
}


int select2(const vector<int> &nums)
UV
{
int k = nums.size()/2;
vector<int> topK(nums.begin(), nums.begin() + k);

sortDec(topK);
for (auto i = k; i < nums.size(); i++)
IA
{
if (nums[i] > topK[k-1])
{
for (auto j = k-2; j >=0 ; j--)
if (nums[i] < topK[j])
_A
{topK[j+1] = nums[i]; break;}
else
topK[j+1] = topK[j];
if (topK[0] < nums[i])
topK[0] = nums[i];
}
}
PP
return topK[k-1];
}

int main()
{
vector<int> nums;
RO
int selected;
time_t start, end;

srand(time(NULL));
for (auto numInts = 1000; numInts<=10000; numInts+=1000)
// sizes 1,000, 2,000, 3,000, ...10,000
{
VE
nums.resize(numInts);

start = time(NULL);
for (auto i = 0; i < 10; i++) // run 10 times
{
for (auto j = 0; j < numInts; j++)
D?
nums[j] = rand()%(2*numInts);
selected = select1(nums); // or selected = select2(nums);
}
end = time(NULL);
cout<<numInts<<"\t"<<difftime(end,start)<<endl;
}
??
return 0;
}

, 180

160
ST
140

120

100
Time of Select1
UV
80 Time of Select2
60

40

20
IA
0
0 2000 4000 6000 8000 10000 12000

2. /*
_A
Word Puzzle problem
from the example in figure 1.1
*/

#include<iostream>
#include<fstream>
PP
#include<string>
#include<vector>
#include "matrix.h"
#include<algorithm>

using namespace std;
RO
const int MAXROWS = 4;
const int MAXCOLS = 4;

struct Orientation
{
Orientation() : delRow(0), delCol(0) {}
VE
Orientation operator() (int direction)
{
switch (direction)
{
case 0 : delRow = -1; delCol = -1; break;
case 1 : delRow = -1; delCol = 0; break;
case 2 : delRow = -1; delCol = 1; break;
D?
case 3 : delRow = 0; delCol = -1; break;
case 4 : delRow = 0; delCol = 1; break;
case 5 : delRow = 1; delCol = -1; break;
case 6 : delRow = 1; delCol = 0; break;
case 7 : delRow = 1; delCol = 1; break;
}
??
return *this;
}
int delRow;
int delCol;

Información del documento

Subido en
7 de noviembre de 2025
Número de páginas
154
Escrito en
2025/2026
Tipo
Examen
Contiene
Preguntas y respuestas
$20.49

¿Documento equivocado? Cámbialo gratis Dentro de los 14 días posteriores a la compra y antes de descargarlo, puedes elegir otro documento. Puedes gastar el importe de nuevo.
Escrito por estudiantes que aprobaron
Inmediatamente disponible después del pago
Leer en línea o como PDF

Seller avatar
Los indicadores de reputación están sujetos a la cantidad de artículos vendidos por una tarifa y las reseñas que ha recibido por esos documentos. Hay tres niveles: Bronce, Plata y Oro. Cuanto mayor reputación, más podrás confiar en la calidad del trabajo del vendedor.
StudyStream
3.9
(15)
Vendido
125
Seguidores
30
Artículos
1230
Última venta
3 semanas hace



Por qué los estudiantes eligen Stuvia

Creado por compañeros estudiantes, verificado por reseñas

Calidad en la que puedes confiar: escrito por estudiantes que aprobaron y evaluado por otros que han usado estos resúmenes.

¿No estás satisfecho? Elige otro documento

¡No te preocupes! Puedes elegir directamente otro documento que se ajuste mejor a lo que buscas.

Paga como quieras, empieza a estudiar al instante

Sin suscripción, sin compromisos. Paga como estés acostumbrado con tarjeta de crédito y descarga tu documento PDF inmediatamente.

Student with book image

“Comprado, descargado y aprobado. Así de fácil puede ser.”

Alisha Student

Preguntas frecuentes