الفريق العربي للبرمجةأرشيف المنتديات · 2000 – 2023
نسخة أرشيفية للقراءة فقط — التسجيل والمشاركة مغلقان، والمحتوى محفوظ كما كان.

مساعدة في Quick Sort بواسطة Linked List

مغلق
بدأه مجلـد جديـد في 20 أبريل 2007 · 4 رد · 862 مشاهدة · في الأسئلة المجابة
مشاركة: واتساب X فيسبوك تيليجرام
#1 صاحب الموضوع

السلام عليكم ورحمة الله وبركاته

كتبت دالة ال Quick Sort ل Singly Linked List واشتغلت تمام 100% ، ال List تأخذ int ( قائمة ارقام ) ودالة الترتيب ترتبها من الاصغر الى الأكبر تصاعدياً :

Node* LinkedList::partition( Node* low, Node* high )
{
	Node* left;
	Node* right;
	Node* pivot;

	int pivotValue;

	pivotValue = low -> data;

	left = low;
	pivot = left;

	right = high;

	while ( getIndex( left ) < getIndex( right ) )
		{
		while ( left -> data <= pivotValue && getIndex( left ) <= getIndex( high ) )
			left = left -> next;

		while ( right -> data > pivotValue && getIndex( right ) >= getIndex( low ) )
			right = getPrev( right );

		if ( getIndex( left ) < getIndex( right ) )
			swap( left, right );
		}
	low -> data = right -> data;
	right -> data = pivotValue;

	return right;
}

void LinkedList::quickSort( Node* first, Node* last )
{
	Node* pivot;
	if ( getIndex( last ) > getIndex( first ) )
		{
		pivot = partition( first, last );
		quickSort( first, getPrev( pivot ) );
		quickSort( pivot -> next, last );
		}
}

وهذي هي الدوال المساعدة وهي getPrev و swap و getIndex

void LinkedList::swap( Node* n1, Node* n2 )// swap data only
{
	int tempValue;
	tempValue = n1 -> data;
	n1 -> updateData( n2 -> data );
	n2 -> updateData( tempValue );
}

Node* LinkedList::getPrev( Node* currentPtr )
{
	Node* current = firstPtr;

	while ( current != NULL )
		{
		if ( current -> next == currentPtr )
			break;

		current = current -> next;
		}

	return current;
}

int LinkedList::getIndex( Node* sd )
{
	Node* current = firstPtr;
	int count = 0;

	while ( current != NULL )
		{
		if ( ( current -> data ) == ( sd -> data ) )
			break;

		count++;
		current = current -> next;
		}
	return count;
}

السؤال او الطلب هو أني أريد ان اجعل دالة الترتيب تقوم بالترتيب من الأكبر الى الأصغر تنازلياً ، قمت ببعض التغييرات في الكود لكن ظهرت لي اخطاء في المؤشرات Segmentation fault ، والذي قمت بتغييره فقط إشارة الأكبر والأصغر وتحديداً هذا الكود :

while ( getIndex( left ) < getIndex( right ) )
		{
		while ( left -> data > pivotValue && getIndex( left ) <= getIndex( last ) )
			left = left -> next;

		while ( right -> data <= pivotValue && getIndex( right ) >= getIndex( first ) )
			right = getPrev( right );

		if ( getIndex( left ) < getIndex( right ) )
			swap( left, right );
		}

وهو موجود داخل دالة partition .

ارجو ان يساعدني أحد في حل هذه المشكلة وشكراً :)

#2

........ ؟

#3

انا اشوف ان من اصعب طرق الsort هي الquick sort احنا التيجر عطتنا الكود حقه بدور عليه عندي واجيبه لك

#4

سيدي ممكن لو سمحت تطبقها على الmerge sort?

#5

ايليان :

مافيه شي صعب اذا كان عندك compiler .. ودكتور مثل د.سمير :)

فانتوم :

باذن الله ، بس أخلص الأول .

هذا الموضوع مغلق.

مواضيع مشابهة