问题描述
我正在学习算法第一部分的课程,并且使用C ++实现了合并排序。应该执行就地排序,但不对数组进行排序。我找不到原因。希望能得到积极的回应。谢谢。
@utils.retry(exception.SnapshotLimitReached,retries=1)
def _create_from_image(self,context,volume,image_location,image_id,image_Meta,image_service,**kwargs):
LOG.debug("cloning %(volume_id)s from image %(image_id)s "
" at location %(image_location)s.",{'volume_id': volume.id,'image_location': image_location,'image_id': image_id})
virtual_size = image_Meta.get('virtual_size')
if virtual_size:
virtual_size = image_utils.check_virtual_size(virtual_size,volume.size,image_id)
# Create the volume from an image.
#
# First see if the driver can clone the image directly.
#
# NOTE (singn): two params need to be returned
# dict containing provider_location for cloned volume
# and clone status.
# NOTE (lixiaoy1): Currently all images are raw data,we can't
# use clone_image to copy data if new volume is encrypted.
volume_is_encrypted = volume.encryption_key_id is not None
cloned = False
model_update = None
if not volume_is_encrypted:
model_update,cloned = self.driver.clone_image(context,image_service)
# Try and clone the image if we have it set as a glance location.
if not cloned and 'cinder' in CONF.allowed_direct_url_schemes:
model_update,cloned = self._clone_image_volume(context,image_Meta)
# If we're going to try using the image cache then prepare the cache
# entry. Note: encrypted volume images are not cached.
if not cloned and self.image_volume_cache and not volume_is_encrypted:
# If _prepare_image_cache_entry() has to create the cache entry
# then it will also create the volume. But if the volume image
# is already in the cache then it returns (None,False),and
# _create_from_image_cache_or_download() will use the cache.
model_update,cloned = self._prepare_image_cache_entry(
context,image_service)
# Try and use the image cache,and download if not cached.
if not cloned:
model_update = self._create_from_image_cache_or_download(
context,image_service)
self._handle_bootable_volume_glance_Meta(context,image_id=image_id,image_Meta=image_Meta)
return model_update
void merge(int *a,int *aux,int low,int mid,int high) {
for (int k = low; k <= high; ++k)
aux[k] = a[k]; // copy
int i = low,j = mid + 1;
for (int k = low; k <= high; ++k) { // merge
if (i > mid)
a[k] = aux[j++];
else if (j > mid)
a[k] = aux[i++];
else if (aux[i] > aux[j])
a[k] = aux[j++];
else
a[k] = aux[i++];
}
}
void sort(int *a,int high) {
if (high <= low)
return;
int mid = low + (high - low) / 2;
sort(a,aux,low,mid);
sort(a,mid + 1,high);
merge(a,mid,high);
}
解决方法
您的合并功能在逻辑上不正确。对于任何迭代,将永远不会执行最后两个条件。 j
将始终大于mid
,控制权将不会超出第二个表达式。
将合并功能的定义修改为类似这样的内容:
void merge(int *a,int *aux,int low,int mid,int high) {
for (int k = low; k <= high; ++k)
aux[k] = a[k]; // copy
int i = low,j = mid + 1;
for (int k = low; k <= high; ++k) { // merge
// changes begin here
if (i <= mid and j <= high) {
if (aux[i] > aux[j])
a[k] = aux[j++];
else
a[k] = aux[i++];
}
else if (i > mid)
a[k] = aux[j++];
else // if (j > high)
a[k] = aux[i++];
// changes end here
}
}